Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-07-27
13
Medium Priority
?
132 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
13 Comments
 
LVL 111

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 111

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
Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

 

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 59

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 59

Accepted Solution

by:
Julian Hansen earned 2000 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 59

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 59

Expert Comment

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

Featured Post

Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

610 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