3

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);
3
  • Applying the_content filter should be sufficient - I'm guessing Visual Composer registers some of its shortcode conditionally i.e. not when your AJAX fires. Commented May 18, 2016 at 9:27
  • I tried it with shortcodes of other plugins and they don't work as well unfortunaltely Commented May 18, 2016 at 9:50
  • That's because all the above work is hook into init which is always fired before wp hook which content filters and shortcodes are hooked into.. Try this paste and let me know if that would be the answer pastebin.com/KbBuzjVi
    – Ismail
    Commented May 18, 2016 at 11:04

2 Answers 2

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();. 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' );
1

the_content is usually "executed" in the context of a loop when all relevant global data is set. You should try to mimic it by using code like

function get_page_content(){
    global $post;

    $id = $_REQUEST['id'];

    $post = get_page($id);
    setup_postdata($post);
      echo apply_filters('the_content', $post->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' );

This may or may not be enough depending on the specific checks the plugin does, and you might need to construct the main query object probably by using query_posts (one of the very rare cases in which it is ok to use that function).

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.