1

I am trying to learn wordpress and php development. I have been trying to create my own shortcode to add a json pull that I can parse into scheduled posts. I have the base php working - I've been experimenting in phpfiddle.org but as soon as I add anything specific to wordpress things stop. I am posting this as a new question because it seems like the latest WP update is causing a lot of issues.

I am adding this code to functions.php:

add_shortcode('aggrenda', 'aggrenda_func');
function requestAggrendaEvents() {
    $json = file_get_contents("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'], '<br>';
echo $postInfo['description'], '<br>';

// See what exactly is in $postInfo right now
var_dump($postInfo);

I'm sure i'm missing something small. Can anyone help?

1

Try removing the brackets and adding the full url:

$url = 'http://theurl.com';

And for clarity, it's best to use different names for the tag and function name:

add_shortcode('aggrenda', 'aggrenda_func');
function aggrenda_func() {

    $url = 'http://theurl.com';

    $response = wp_remote_get( $url );

    echo($response);
}
  • Awesome! Thank you for the response. I did that and am now only getting a response with "array"... – mpellas Aug 24 '15 at 1:22
  • OK...I used print_r instead of echo and i can now see the array. Next, I have to try and parse it. – mpellas Aug 24 '15 at 1:27
  • Please accept the answer. wordpress.stackexchange.com/help/accepted-answer – shanebp Aug 24 '15 at 13:56
  • Sorry I didn't earlier...another rookie mistake on my part. – mpellas Aug 24 '15 at 14:58
  • I edited my orginal post for updated code. I tried it in phpfiddle.org and it works fine at pulling the string. I use it in functions.php and it pulls the string but not in a post...just a blank page shows up with the json string. If I change file_get_contents with wp_remote_get I get a null return. I need this JSON array in a WP post... – mpellas Aug 24 '15 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.