• Hello,

    I’ve got a handle on the non Worspress PHP (I’ve been experimenting in PHPfiddle.org) but am having trouble translating it to functionality in wordpress.

    I’m looking to get a json pull from http://aggrenda.com/mpellas/michael-pellas/events.json and post the data (address, description, photo url, website) in a scheduled wordpress post.

    I’ve been able to get a json string but haven’t been able to parse it in wordpress.

    here is the latest code:

    add_shortcode('aggrenda', 'requestAggrendaEvents');
    
    		function requestAggrendaEvents() {
    			$json = wp_remote_get("http://aggrenda.com/mpellas/michael-pellas/events.json");
    			$data = json_decode($json, true);
    			return $data['events'];
    		}
    
    		function getPostInfo($aggrendaEvent) {
    			return array(
    				"title" => $aggrendaEvent['title'],
    				"description" => $aggrendaEvent['description'],
    			);
    		}
    
    		// Get the aggrenda events as an associative array
    		$aggrendaEvents = requestAggrendaEvents();
    
    		// Get the WordPress post information for the "next" event in Aggrenda
    		$postInfo = getPostInfo($aggrendaEvents[0]);
    
    		// TODO: Create a post using $postInfo
    		echo $postInfo['title'], '';
    		echo $postInfo['description'], '';
    
    		// See what exactly is in $postInfo right now
    		var_dump($postInfo);

    What am I missing?

Viewing 1 replies (of 1 total)
  • mpellas

    wp_remote_get returns you with an array, not JSON encoded string as you’re expecting, that goes for “What am I missing” part.

    Same thing happened with me when I first used wp_remote_get, then I learnt reading codex

    Also, WordPress functions are not available in PHPFiddle(or thats what I believe).

    Your code will work in a WordPress site, after proper handling of data returned by wp_remote_get.

    Do let me know if it worked or not for you.

Viewing 1 replies (of 1 total)

The topic ‘json pull to insert into a post’ is closed to new replies.