I have a simple function, which returns the content of a page with the given pageID:

function get_page_content(){

    $id = $_REQUEST['id'];

    $page_data = get_page($id);

      echo apply_filters('the_content', $page_data->post_content);
    //echo do_shortcode($page_data -> post_content);
    wp_die();
}
add_action( 'wp_ajax_nopriv_get_page_content', 'get_page_content' );
add_action( 'wp_ajax_get_page_content', 'get_page_content' );

But after an Update of WP and some Plugins the returned content still containes unresolved shortcodes like so:

[vc_row row_type=“row“ use_row_as_full_screen_section=“no“ type=“grid“ text_align=“left“ background_image_as_pattern=“without_pattern“][vc_column width=“1/1″]

The shortcodes come from a Plugin called Visual Composer (which got updated in the process to the latest version)

Question:
How can i render the shortcodes befor i return the content?
I tried both

echo apply_filters('the_content', $page_data->post_content);

and

echo do_shortcode($page_data -> post_content);

2 Answers
2

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();. So below code may help you,

function get_page_content(){

    $id = $_REQUEST['id'];

    $page_data = get_page($id);

    WPBMap::addAllMappedShortcodes();

    echo apply_filters('the_content', $page_data->post_content);
    wp_die();
}
add_action( 'wp_ajax_nopriv_get_page_content', 'get_page_content' );
add_action( 'wp_ajax_get_page_content', 'get_page_content' );

Leave a Reply

Your email address will not be published. Required fields are marked *