Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
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 '12 at 17:46

1 Answer 1

up vote 0 down vote accepted

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

jQuery.ajax({
    url: yourSiteUrl + "/wp-admin/admin-ajax.php",
    dataType: 'json',
    data: {
       'action':'your_ajax',
       'fn':'run_shortcode_function',
       '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'

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

     }

   // 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

          $output=json_encode($output);
         if(is_array($output)){
        print_r($output);
         }
         else{
        echo $output;
         }
         die;

}
your_ajax_shortcode_function($some_needed_value){
     return the_specific_function_that_the_shortcode_was_pointing_to($some_needed_value);
}   

I hope this points you in the right direction.

share|improve this answer

Your Answer

 
discard

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.