Solved

do_shortcode() returns only the inputted string itself rather than the output I'd expect

Posted on 2016-07-27
13
87 Views
Last Modified: 2016-08-15
OK, first off, I'm a bit of a web programming newbie and this may be something obvious.

The following code behaves *correctly* when rendered from a php file I launch from Wordpress. Well, actually it only worked when I entered the line:

require "shortcodes.php";   at the beginning

<?php
$var = do_shortcode('[nggallery id=1]');
echo $var                
?>

Open in new window


So far, so good.

Now, the almost precise same code in another php file, called from javascript hence the json encoding, just seems to return the literal string passed in "[nggallery id=1]"

<?php
require "shortcodes.php";
$var = do_shortcode('[nggallery id=1]');
echo json_encode($var);                
?>

Open in new window


Why the difference? Clearly the theme is working because one of them behaves. Do I need to include/require more files somewhere?

Thanks loads
Iain
0
Comment
Question by:iaing1000
  • 7
  • 4
  • 2
13 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41731983
Are you using a framework or CMS, such as WordPress?  If so, you might use the Request Attention button and ask a moderator to add this question to the WordPress Zone.
0
 

Author Comment

by:iaing1000
ID: 41731987
Yes, am using WordPress and have just followed the link to Request Attention. Thanks for letting me know.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41732013
That should help get some good expert eyes on the problem!  I know a little about WordPress, but there are others who are much more knowledgeable, and I'm sure one of them will come along shortly! :-)
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:iaing1000
ID: 41732024
Thanks you're a good sort! I'm stressing here over this at work and am totally stuck now :-(
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41732443
What do you get if you call the second script directly?

This should not have anything to do with JavaScript - you need to get the script working correctly using a URL in your browser - once that is working then you can do the JS test.

Can you do the browser test and let us know what is displayed - particularly any errors.
0
 

Author Comment

by:iaing1000
ID: 41741855
Hi, sorry for delay I'm away at the moment! Will respond in Monday when I'm back, really appreciate your help, thanks
0
 

Author Comment

by:iaing1000
ID: 41752219
Hi Julian,

Sorry for the long delay while I've been away!

If I run the shortcode for the gallery (which has no images in it for now) this is the output in the browser. It will obviously be more involved once there are images present but this seems to serve to illustrate:

<p><p>no images were found</p></p>

Is thiswhat you meant?

Thanks
Iain
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41752245
<p><p>no images were found</p></p>

Open in new window


This appears to be plain HTML output yet your script is attempting to output JSON?

echo json_encode($var);  

Open in new window


It seems there is a disconnect somewhere.

In your first script you are just doing an echo $var which implies that the output from do_shortcode is just plain html.

Therefore, sending back as JSON does not make sense.

I would imagine your JavaScript would have to make the call to the script as a normal text/html return and then simply insert the returned data into the DOM.

Going forward.

1. What output does do_shortcode produce - can you post that
    Change your second script just to do an echo $var instead of the json_encode

2. What does your JavaScript look like - that calls this script?
0
 

Author Comment

by:iaing1000
ID: 41756455
Hi,

Sorry for the continuing delays, we're spending so much time doing other stuff!

As it happens the echo $var returns the same, and is just the literal string "[nggallery id=1]". Very odd.

However, we've now ditched doing this in WordPress altogether, and hence the shortcodes along with it. This is because the plugin we're writing will now need to work in any number of alternative client set ups and we'll just be providing an iFrame that our cusotmers can hook into their existing site. I think we're going to use API calls to the various tools we use and get more into the PHP.

It's been a pretty hefty learning curve that's for sure as the goalposts keep moving on this project!

What's the best way to close this off and assign solutions?

Thanks again
Iain
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41756489
You will need to decide if the answers helped you to find a resolution to your problem - if so you should close the question and assign points to the comment that best helped you reach a resolution.

If you feel that no comments really helped you with your problem you can request the question be deleted.

The rule of thumb is that not all questions have to lead to an answer - sometimes no answer is an answer.

In this case it is your call - I can't make that decision for you.
0
 

Author Closing Comment

by:iaing1000
ID: 41756498
In the end I didn't require a solution to this because our project changed. However, it's clear that this would shed some light on how to then tackle the solution.
0
 

Author Comment

by:iaing1000
ID: 41756499
OK, thanks Julian :-)
0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 41756506
You are most welcome.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question