WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm wrangling a little logic problem:

I'm making a single page WP theme which uses the JSON API for the content and Mustache style templates for the presentation... However, shortcodes? What would be the best way to handle these?

For example:

JSON result->template->DOM injection->trigger hijack for gallery/slideshow/whatever the shortcode is?

The above method feels a bit dirty as I'd have to either keep firing a parser to process the shortcodes or else I'd have to use JQuery.live (seriously dirty).

Can anyone think of a more elegant way of doing this?

share|improve this question
up vote 3 down vote accepted

You can add your own AJAX API for do_shortcode. Add this to a suitable location (i.e. functions.php or a plugin):

add_action('wp_ajax_doshortcode', 'ajax_doshortcode');
function doshortcode() {
  echo do_shortcode($_POST['text']);
  die(); // this is required to return a proper result

And this to your Javascript:

  url : ajaxurl,
  data : { action : 'doshortcode', text : <text> },
  type : 'POST',
  error : function(req, stat, err) {...},
  success : function(data, stat, req) {...}

ajaxurl is defined on admin pages; see here for instructions for viewer-side applications.

Alternatively, you can set up such actions for all API functions you need, wrapping the original calls with do_shortcode.

share|improve this answer
The answer seems a bit vague. We tried the answer and could not get a solution. Could you elaborate a bit more? – user27391 Feb 13 '13 at 4:27
@Antonio I have no idea whether this still works in the same way, and I have not worked with WP for some time. Sorry. Maybe post a new question linking to this? The current herd of users might be able to help you more. – Raphael Feb 13 '13 at 14:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.