I want to display a shortcode through jquery after the DOM is loaded:

This is how I call the shortcode: <?php echo do_shortcode('[plugin]'); ?>

Now, my question is how can I call that shortcode inside a jquery function, because my website is based on jquery/ ajax calls? Thanks!

  • 1
    Please search the site. You're simply _doing_it_wrong(). There's no need to call a shortcode - just call the underlying functions/AJAX callbacks or access the localized object (if there is one ready).
    – kaiser
    Sep 3, 2012 at 17:46

1 Answer 1


Javascript code is running in the user's browser and not on your server (where your wordpress content resides). You could use ajax to call the function which the shortcode is pointing to.

Here is how I handle AJAX calls with Wordpress:

1) I use a jQuery ajax function to call the wp-admin/ajax.php

    url: yourSiteUrl + "/wp-admin/admin-ajax.php",
    dataType: 'json',
    data: {
       'some_needed_value': jQuery('#input_for_needed_value').val()
    success: function(results){
        //do something with the results of the shortcode to the DOM
    error: function(errorThrown){console.log(errorThrown);}
});// end of ajax

2) This PHP code is located in the functions.php file of your theme or in a custom plugin:

//this is wordpress ajax that can work in front-end and admin areas
add_action('wp_ajax_nopriv_your_ajax', 'your_ajax_function');
add_action('wp_ajax_your_ajax', 'your_ajax_function');
function your_ajax_function(){
     // the first part is a SWTICHBOARD that fires specific functions according to the value of Query Variable 'fn'

        case 'run_shortcode_function':
           $output = your_ajax_shortcode_function($_REQUEST['some_needed_value']);
        case 'some_other_function_you_want_to_perform':   
           $output = some_other_function($_REQUEST['first_name'],$_REQUEST['last_name']);
          $output = 'No function specified.';


   // at this point, $output contains some sort of data that you want back
   // Now, convert $output to JSON and echo it to the browser
   // That way, we can recapture it with jQuery and run our success function

        echo $output;

     return the_specific_function_that_the_shortcode_was_pointing_to($some_needed_value);

I hope this points you in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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