1

I used an ajax request in Wordpress to get the content of a post, in that post i used the Visual Composer. but the content shows only the VC shortcodes with out changing them to the real content.. That is the code that i used

add_action( 'wp_ajax_drpk_custom','drpk_custom' );
add_action( 'wp_ajax_nopriv_drpk_custom','drpk_custom' );
function drpk_custom(){
if(isset($_REQUEST)){
    $id = $_REQUEST['id'];

    $query = new WP_Query(array('p'=>$id));
    if($query->have_posts()):
        while($query->have_posts()): $query->the_post();?>
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><?php the_title() ?></h4>
      </div>
      <div class="modal-body">
         <?php the_content() ?>
      </div>
        <?php endwhile;

    endif;
}
wp_die();  }

And this jQuery code

    $('.cart-item').find('a').on('click',function(){
    var postID      = $(this).data('id'),
        ajaxContent = $('.modal-content');
        $.ajax({
            url: ajaxUrl.url,
            data: {
                'action' : 'drpk_custom',
                'id': postID
            },
            beforeSend: function(){
                // $load.fadeIn(500);
            },
            success: function(data){
                // $load.hide();
                ajaxContent.html(data);
            }
        }); 
    });

but returns like that

[vc_row][vc_column width=”1/4″][vc_single_image image=”389″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”390″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”391″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][vc_column width=”1/4″][vc_single_image image=”392″ img_size=”full” alignment=”center” onclick=”custom_link” img_link_target=”_blank” link=”#”][/vc_column][/vc_row]
Sven
  • 69,403
  • 10
  • 107
  • 109

3 Answers3

7

Since version 4.9 visual composer added shortcode lazy loading. To use VC shortcodes on AJAX content use this function before printing the content WPBMap::addAllMappedShortcodes();

add_action( 'wp_ajax_drpk_custom','drpk_custom' );
add_action( 'wp_ajax_nopriv_drpk_custom','drpk_custom' );
function drpk_custom(){
if(isset($_REQUEST)){
    $id = $_REQUEST['id'];

    $query = new WP_Query(array('p'=>$id));
    if($query->have_posts()):
        while($query->have_posts()): $query->the_post();
            WPBMap::addAllMappedShortcodes();
        ?>
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"><?php the_title() ?></h4>
      </div>
      <div class="modal-body">
         <?php the_content() ?>
      </div>
        <?php endwhile;

    endif;
}
wp_die();  }
Dhinju Divakaran
  • 893
  • 1
  • 8
  • 11
  • 1
    Calling `WPBMap::addAllMappedShortcodes()` before `the_content()` won't work for me in AJAX theme, and no errors are shown in browser console. – Marco Marsala Nov 03 '16 at 10:20
3
if(class_exists('WPBMap') && method_exists('WPBMap', 'addAllMappedShortcodes')) { // 1.17c. FIxes issues with ajax hopefully.

                WPBMap::addAllMappedShortcodes(); // this is needed to ensure wc shortocdes come out right.

            }

This would be what you'd want to do. I don't know why personally, but it works so long as you use a method and class check prior to initiating the call.My guess would be something to do with the filters/hooks run prior to the ajax callback.

Additionally, it should be noted that if you're running a loop outside the main query via ajax, you want to run this after every $query->the_post() as this sets up context for functions like the_title();

Hybrid web dev
  • 328
  • 4
  • 15
1

To expand on above solutions, if the post you're using has custom css style changes like border, background, etc.. you'll need to get that custom css too using get_post_meta($pid, '_wpb_shortcodes_custom_css', true);

Example:

$thepost = get_post($pid);
WPBMap::addAllMappedShortcodes();
$content = $thepost->post_content;
$content = apply_filters('the_content', $content);
$vccss = get_post_meta($pid, '_wpb_shortcodes_custom_css', true);
if(!empty($vccss)) 
{
    $vccss = strip_tags($vccss);
    $content.='<style type="text/css" data-type="vc_shortcodes-custom-css">';
    $content.=$vccss;
    $content.='</style>';
}           
echo $content;
Chris
  • 893
  • 10
  • 23