[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

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

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
iaing1000
Asked:
iaing1000
  • 7
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
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
 
iaing1000Author Commented:
Yes, am using WordPress and have just followed the link to Request Attention. Thanks for letting me know.
0
 
Ray PaseurCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
iaing1000Author Commented:
Thanks you're a good sort! I'm stressing here over this at work and am totally stuck now :-(
0
 
Julian HansenCommented:
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
 
iaing1000Author Commented:
Hi, sorry for delay I'm away at the moment! Will respond in Monday when I'm back, really appreciate your help, thanks
0
 
iaing1000Author Commented:
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
 
Julian HansenCommented:
<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
 
iaing1000Author Commented:
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
 
Julian HansenCommented:
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
 
iaing1000Author Commented:
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
 
iaing1000Author Commented:
OK, thanks Julian :-)
0
 
Julian HansenCommented:
You are most welcome.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 7
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now