Skip to main content
added 2182 characters in body
Source Link

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

in functions.php

//  AJAX HOOKS TO LOAD PRODUCTS' INFO
add_action('wp_ajax_get_product_info', 'get_product_info' );
add_action('wp_ajax_nopriv_get_product_info', 'get_product_info' );

// SHOULD RUN SHORTCODE TO GET PRODUCT INFORMATION AND SEND IT BACK TO JS SCRIPT
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
   exit;
}

// 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 );
                
                wp_enqueue_script('product-ajax');
                
                wp_localize_script( 
                    'product-ajax', 
                    'productajaxvars', 
                    array( 
                        '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() {
      
        $.ajax({
            url:  productajaxvars.ajax_url,
            data: { 
                action:  productajaxvars.callback,  // callback defined in functions.php
                nonce :  productajaxvars.nonce,
                product_id: 82},
                success: function( response ) {
                            alert(response);
                },
                type: "POST",
        });
         
              
    });
})( jQuery );

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

in functions.php

//  AJAX HOOKS TO LOAD PRODUCTS' INFO
add_action('wp_ajax_get_product_info', 'get_product_info' );
add_action('wp_ajax_nopriv_get_product_info', 'get_product_info' );

// SHOULD RUN SHORTCODE TO GET PRODUCT INFORMATION AND SEND IT BACK TO JS SCRIPT
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
   exit;
}

// 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 );
                
                wp_enqueue_script('product-ajax');
                
                wp_localize_script( 
                    'product-ajax', 
                    'productajaxvars', 
                    array( 
                        '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() {
      
        $.ajax({
            url:  productajaxvars.ajax_url,
            data: { 
                action:  productajaxvars.callback,  // callback defined in functions.php
                nonce :  productajaxvars.nonce,
                product_id: 82},
                success: function( response ) {
                            alert(response);
                },
                type: "POST",
        });
         
              
    });
})( jQuery );
Source Link

AJAX and do_shortcode

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.