I know questions on this subject have been asked before. But I'm stuck and cannot find a way out.

I am using Ajax to load external information to my site. Initially all the information was loaded at once, but because of the large amount of data Ajax became a necessity.

The information is accessed from an external CMS, using a plugin developed by a 3rd party.

In the Ajax I have to use the plugin's do_shortcode. But it fails. No results are returned. When it is directly put in a page template's code it works flawlessly.

I have used the best practices I found to create the Ajax communication (i.e., use wp_ajax and wp_ajax_nopriv for the callbacks, registering, enqueueing, and localization of JS scripts).

I tested the Ajax connection and I can send messages back and forth. But as stated, the shortcode is the one thing that always fails.

I haven't been able to reach to the plugin developer. So I'm asking if there are any other things I should be aware of when trying to use shortcodes with wp_ajax.

Thanks in advance.

EDIT: I'm adding code so that it can help:

in functions.php

add_action('wp_ajax_get_product_info', 'get_product_info' );
add_action('wp_ajax_nopriv_get_product_info', 'get_product_info' );

function get_product_info() {
   //Checks the nonce and kills the script if wrong
   $nonce = $_POST['nonce'];
   if ( ! wp_verify_nonce( $nonce, 'return_posts' ) )
        die ( 'Wrong nonce!');
   //Process Info
   $pid = $_POST['product_id'];   
   header('Content-type: text/html');

   // Returns the shortcode
   echo do_shortcode("[ps_product_list id_product=".$pid." tpl=product-grid-exclusives.tpl]");

    // We don't want anything else to run

// REGISTERS, LOADS & LOCALIZES product-store-ajax.js
if (!function_exists('load_scripts')) {
    function load_scripts() {

        if ( !is_admin() && is_category('teaser-online-exclusives') ) {

                wp_register_script( 'product-ajax', get_stylesheet_directory_uri().'/js/product-store-ajax.js', array( 'jquery' ), '', true );


                        'ajax_url' =>  admin_url( 'admin-ajax.php' ), 
                        'nonce'    =>  wp_create_nonce( 'return_posts' ),
                        'callback' => 'get_product_info'
add_action('wp_enqueue_scripts', 'load_scripts');

And the JS file:

(function( $ ) {
    $(function() {

            url:  productajaxvars.ajax_url,
            data: { 
                action:  productajaxvars.callback,  // callback defined in functions.php
                nonce :  productajaxvars.nonce,
                product_id: 82},
                success: function( response ) {
                type: "POST",

})( jQuery );
  • 2
    It will be tough to answer the question without more details. Do you have any code you can provide? Commented Nov 11, 2015 at 23:03
  • Hi Thans, I edited the question to include the code. Commented Nov 12, 2015 at 16:33

1 Answer 1


Ajax runs without a main WP_Query object and therefor all its APIs that are used to determine the type of page that is being displayed. The code which is responsible for outputting the result of the shortcode is probably checking for is_single() to be true so it will not add output to archive pages.

What you can do is to create a main query object, but it will probably be just faster to look at the code that generates the shortcode and find out if there is a simple function you can calll to do what you need.

A note about your general strategy - you are going to overload your sever with uncachable requests. If you have the CPU to spare or it is just one page, then there is probably no problem with that, but otherwise be very careful with what you do.

  • Thanks Mark for your answer. I'm looking into other alternatives. Regarding your last point, would it help to cache the AJAX requests? Or do you see a different problem? Commented Nov 12, 2015 at 19:13
  • It is hard to give general advice here. just remember that even with caching the ajax every page load will generate two requests instead of one. If you can cache the ajax response in nginx/varnish/cloudflare/whatever then it is not a big things, but if it gets to PHP level the cost is high from just spinning up php and bootstrapping wordpress. Commented Nov 12, 2015 at 20:26
  • @Mark, thank you for all the advice. I'll try to optimize my solution. Commented Nov 15, 2015 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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