ansonphong / postworld

Wordpress Theme Development Framework
GNU General Public License v2.0
7 stars 0 forks source link

wp_insert_terms() - Error #65

Closed ansonphong closed 11 years ago

ansonphong commented 11 years ago

Hi Haidy, So I'm trying to use pw_insert_terms() here to insert an array of terms into the DB - this will be a very helpful function.

Although, what happens is nothing happens when I run it.

So I insert it like this, with a well formed JSON object.

$add_terms = '{ "topic" : [ { slug:"psyche", name:"/psyche", children:{ ancient:"Ancient Mysteries", astrology:"Astrology", consciousness:"Consciousness", dreams:"Dreams", ets:"Extraterrestrials", indigenous:"Indigenous Cultures", occult:"Occult", psi:"Psi", psychedelics:"Psychedelics", psychology:"Psychology", shamanism:"Shamanism", spirituality:"Spirituality", transformation:"Transformation", psyche_misc:"Misc" }, }, { slug:"arts", name:"/arts", children:{ conferences:"Conferences", digital_art:"Digital Art", world_art:"World Art", festival_culture:"Festival Culture", games:"Games", music:"Music", performance:"Performance", publications:"Publications", video:"Video", film:"Film", misc:"Misc" }, }, { slug:"body", name:"/body", children:{ energy_medicine:"Energy Medicine", food_nutrition:"Food & Nutrition", healing:"Healing", herbalism:"Herbalism", homeopathy:"Homeopathy", sexuality:"Sexuality", slow_living:"Slow Living", tantra:"Tantra", yoga:"Yoga", body_misc:"Misc", }, }, { slug:"eco", name:"/eco", children:{ animal_rights:"Animal Rights", climate_change:"Climate Change", conservation:"Conservation", energy:"Energy", environment:"Environment", extinction:"Extinction", gardening:"Gardening", permaculture:"Permaculture", sustainability:"Sustainability", water:"Water", eco_misc:"Misc", }, }, { slug:"tech", name:"/tech", children:{ biomimicry:"Biomimicry", cosmology:"Cosmology", design_science:"Design Science", digital:"Digital", diy:"DIY", networks:"Networks", privacy:"Privacy", robotics:"Robotics", singularity:"Singularity", tech_misc:"Misc" } }, { slug:"commons", name:"/commons", children:{ action_alerts:"Action Alerts", activism:"Activism", alternative_economics:"Alternative Economics", collaboration:"Collaboration", community:"Community", crowdfunding:"Crowdfunding", democracy:"Democracy", drug_laws:"Drug Laws", evolver:"Evolver", evolver_spores:"Evolver Spores", open_source:"Open Source", peer_to_peer:"Peer to Peer", retreats:"Retreats", commons_misc:"Misc" } }, ], "section" : [ { slug:"psychedelic", name:"Psychedelic Culture", }, { slug:"conscious_convergences", name:"Conscious Convergences", }, { slug:"psi", name:"Psi Frontiers", }, { slug:"video", name:"Videos", }, { slug:"podcast", name:"Podcasts", }, { slug:"edm", name:"Evolver EDM", }, { slug:"evo_network", name:"Evolver Network", }, { slug:"evo_learning_lab", name:"Evolver Learning Lab", }, ], "type" : [ { slug:"song_week", name:"Song of the Week", parent_name:"Hilight", parent:"hilight", }, { slug:"video_week", name:"Video of the Week", parent_name:"Hilight", parent:"hilight" }, { slug:"event_feature", name:"Featured Event", parent_name:"Events", parent:"events" }, { slug:"event_evolver", name:"Evolver Event", parent_name:"Events", parent:"events" }, ], }';

pw_insert_terms( $add_terms, "JSON", true );

But then this is the error that is coming :

Warning: mysql_connect(): Access denied for user 'haidy'@'localhost' (using password: YES) in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-includes/wp-db.php on line 1142

Any ideas?

hmikhail commented 11 years ago

When I do json_decode to your json, and run json_last_error() it gives number 4 which maps to 4 = JSON_ERROR_SYNTAX.

I think you json should look as follows (so that when decoding the json it gives the specified array structure)

$test ='{
  "topic": [
    {
      "slug": "psyche",
      "name": "\/psyche",
      "children": {
        "ancient": "Ancient Mysteries",
        "astrology": "Astrology",
        "consciousness": "Consciousness",
        "dreams": "Dreams",
        "ets": "Extraterrestrials",
        "indigenousv": "Indigenous Cultures",
        "occult": "Occult",
        "psi": "Psi",
        "psychedelics": "Psychedelics",
        "psychology": "Psychology",
        "shamanism": "Shamanism",
        "spirituality": "Spirituality",
        "transformation": "Transformation",
        "psyche_misc": "Misc"
      }
    },
    {
      "slug": "eco",
      "name": "\/eco",
      "children": {
        "animal_rights": "Animal Rights",
        "climate_change": "Climate Change",
        "conservation": "Conservation",
        "energy": "Energy",
        "environment": "Environment",
        "extinction": "Extinction",
        "gardening": "Gardening",
        "permaculture": "Permaculture",
        "sustainability": "Sustainability",
        "water": "Water",
        "eco_misc": "Misc"
      }
    },
    {
      "slug": "commons",
      "name": "\/commons",
      "children": {
        "action_alerts": "Action Alerts",
        "activism": "Activism",
        "alternative_economics": "Alternative Economics",
        "collaboration": "Collaboration",
        "community": "Community",
        "crowdfunding": "Crowdfunding",
        "democracy": "Democracy",
        "drug_laws": "Drug Laws",
        "evolver": "Evolver",
        "evolver_spores": "Evolver Spores",
        "open_source": "Open Source",
        "peer_to_peer": "Peer to Peer",
        "retreats": "Retreats",
        "commons_misc": "Misc"
      }
    }
  ],
  "section": [
    {
      "slug": "psychedelic",
      "name": "Psychedelic Culture"
    },
    {
      "slug": "conscious_convergences",
      "name": "Conscious Convergences"
    },
    {
      "slug": "psi",
      "name": "Psi Frontiers"
    },
    {
      "slug": "video",
      "name": "Videos"
    },
    {
      "slug": "podcast",
      "name": "Podcasts"
    },
    {
      "slug": "edm",
      "name": "Evolver EDM"
    },
    {
      "slug": "evo_network",
      "name": "Evolver Network"
    },
    {
      "slug": "evo_learning_lab",
      "name": "Evolver Learning Lab"
    }
  ],
  "type": [
    {
      "slug": "song_week",
      "name": "Song of the Week",
      "parent_name": "Hilight",
      "parent": "hilight"
    },
    {
      "slug": "video_week",
      "name": "Video of the Week",
      "parent_name": "Hilight",
      "parent": "hilight"
    }
  ]
}';

print_r(pw_insert_terms($test,'JSON', true))
ansonphong commented 11 years ago

Ok great, that seems to be adding them now.

Strangely, they seem to be appearing in some of the places I'm getting them, and other places they're not. Also in the Wordpress Admin, they're not all showing up in the term editor - they show up like... nothing. I'll try and investigate the cause of this.

image

Is this happening to you? Closing the issue for now.

ansonphong commented 11 years ago

Oh yes I see, my JSON was pretty sloppy with extra commas and no \ before /. Thanks.

ansonphong commented 11 years ago

That last issue got fixed now too somehow. Seems to be working well, excellent.

ansonphong commented 11 years ago

Hi Haidy, I've tried copy and pasting your test code exactly, although I haven't been able to get this function to work correctly. It gives this warning every time and no terms are added

Warning: get_object_vars() expects parameter 1 to be object, array given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 246

Warning: array_keys() expects parameter 1 to be array, null given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 247

Warning: get_object_vars() expects parameter 1 to be object, array given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 246

Warning: array_keys() expects parameter 1 to be array, null given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 247

Warning: get_object_vars() expects parameter 1 to be object, array given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 246

Warning: array_keys() expects parameter 1 to be array, null given in /Users/phong/Projects Clients/Reality Sandwich/_WEB/RSV2/WORDPRESS/wp-content/plugins/postworld/php/postworld_taxonomies.php on line 247
hmikhail commented 11 years ago

I solved this issue, but there is another question, what happens if the taxonomy is invalid? In the input specified above, topic and section are invalid taxonomies.

ansonphong commented 11 years ago

If the taxonomy itself is invalid, then just skip over it.

On 2013-11-03, at 3:48 AM, hmikhail notifications@github.com wrote:

I solved this issue, but there is another question, what happens if the taxonomy is invalid? In the input specified above, topic and section are invalid taxonomies.

— Reply to this email directly or view it on GitHub.