Solved

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

Posted on 2016-07-27
13
73 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks you're a good sort! I'm stressing here over this at work and am totally stuck now :-(
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
Hi, sorry for delay I'm away at the moment! Will respond in Monday when I'm back, really appreciate your help, thanks
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:iaing1000
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 500 total points
Comment Utility
<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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
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
Comment Utility
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
Comment Utility
OK, thanks Julian :-)
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
You are most welcome.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 26
onOpen. check dates in column, colour background 6 18
Setting Up Local Lan Web Server 1 12
sql sentence 2 8
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

10 Experts available now in Live!

Get 1:1 Help Now