1

I'm testing an API I build locally with a local install of wordpress.

My API path is something like: http://local.web.tt.com:8615/api/product

With my Node server running it displays this in the browser when you go to that link (mongodb collections object]:

[ { "_id":"54bd5fbb646174009a450001", "productname":"Product 1", "overview":"Overview Title", "benefits": [ "List item 1", "List item 2", "List item 3" ] } ] 

My PHP wordpress shortcode plugin

add_shortcode('product', function($atts, $content) { $service_url = 'http://local.web.tt.com:8615/api/product'; $data = json_decode($service_url, true); return $data; }); 

Basically nothing shows up on the page when I add [product] in a blog post. The shortcode does work if I return just a simple string.

I also just tried this:

add_shortcode('product', function($data) { $service_url = 'http://local.web.tt.com:8615/api/product'; $get = file_get_contents($service_url); $data = json_decode($get, true); return $data; }); 

However that just spits out Array into the spot where the shortcode goes:

enter image description here

What I'm trying to do is capture the strings in each of those keys of the JSON object, then nicely display them with HTML & CSS. ie: The Benefits array items will show up as bullet points.

Basically something like this:

$content .=' <h2>$data.overview</h2> <ul> <li>$data.benefits[0] <li>$data.benefits[1]' return $content; 

Any idea what I'm missing? Thanks in advance!

6
  • You are trying to get data via curl in this case u don't need it, just json_decode() will work fine for you. Commented Jan 19, 2015 at 17:16
  • That's what I tried first, but getting blank... and no errors :( ok will remove the crud part of the post since that isn't needed Commented Jan 19, 2015 at 17:21
  • Maybe you need to return "product = $data"; , im not an wp expert but as i read about this : codex.wordpress.org/Function_Reference/add_shortcode Commented Jan 19, 2015 at 17:29
  • Hmm, tried that and also return "product = {$data['name']}"; but the only thing that shows up in the post is the string product = Commented Jan 19, 2015 at 17:33
  • 1
    should be $data['benefits'][0] Commented Jan 19, 2015 at 19:58

1 Answer 1

1
{ "_id": "84b7e4c7946174009a1f0000", "name": "Name of product", "overview": "Long blah blah blah blah here...", "benefits": [ "List item 1", "List item 2", "List item 3", "List item 4" ] } 

fix your json to have quotes for keys

 $service_url = 'http://local.web.tt.com:8615/api/product'; $get = file_get_contents($service_url); $data = json_decode($get, true); return $data; 

Also, why are you returning the array ?

Sign up to request clarification or add additional context in comments.

3 Comments

Oh I'm getting Array back now! Well I want to display all that data nicely with CSS. The benefits array items are basically going to be bullet points. I changed the keys to have quotes too, but still just getting Array
Ooo I'm getting somewhere with this: stackoverflow.com/questions/16700960/… I can see the var_dump on the page now with the correct data! It's ugly, but just gotta format it now...
I got a bit further and made a new question here, do you think you'd have a moment to check it out? stackoverflow.com/questions/28033267/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.