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">×</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]
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();
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();
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: