Solved

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

Posted on 2016-07-27
13
80 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 108

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 108

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
 

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 52

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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 52

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 52

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Carousel Size 1 15
Session timeout 5 14
Sorting a string 1 13
php construct 5 7
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now