2

I am using AJAX to load next set of posts on homepage. The posts load fine but the shortcodes are not rendered. I am trying to use the do_shortcode function on the content the ajax call fetches (although experts have suggested not to use do_shortcode) but apparently it is failing.

Here's my function that processes the request:

function theme_load_more_posts() {
    check_ajax_referer( 'theme-load-more-posts-nonce', 'nonce' );

    $args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
    $args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
    $args['paged'] = esc_attr( $_POST['page'] );
    $args['post_status'] = 'publish';
    ob_start();
    $loop = new WP_Query( $args );
    if( $loop->have_posts() ): while( $loop->have_posts() ): $loop->the_post();
        get_content_template();
    endwhile; 
    endif;
    $data = ob_get_clean();
    wp_send_json_success( do_shortcode($data) ); // Performing do_shortcode here but it doesn't work.
    wp_reset_postdata();
    wp_die();
}
add_action( 'wp_ajax_theme_load_more_posts', 'theme_load_more_posts' );
add_action( 'wp_ajax_nopriv_theme_load_more_posts', 'theme_load_more_posts' );

The get_content_template() function loads a template file that contains the regular wordpress code for displaying post contents.

I tried using do_shortcode_in_html() on get_content_template() but it doesn't work either.

I also saw similar threads related to this but they mostly addressed issues with the ajax call itself. I believe my problem lies within this function. Any guesses?

  • Have you tried setting your $variable = do_shortcode($data) to a variable first an then sending that $variable to wp_send_json_success( $variable );? – Nathan Powell May 28 '16 at 6:38
  • @NathanPowell I did try that but it doesn't work. – JMDee May 28 '16 at 7:19
  • I have to also mention the ob_get_clean(); as well. The echo could be mucking things up. I am stabbing in the dark obviously, but that looks like slop. – Nathan Powell May 28 '16 at 7:46
  • @NathanPowell Maybe. But no solution seems to be in sight as of now. Thank you for your responses. – JMDee May 28 '16 at 8:34
  • Maybe? Have you tried a return of values for the JavaScript response? – Nathan Powell May 28 '16 at 8:38
0

I believe the problem is you are not calling do_shortcode() in the output buffer. You don't need to be running the loop within the output buffer since I can assume get_content_template() simply returns HTML with embedded shortcodes. Save that to a string variable, then run that through the do_shortcode() and save the echoed output to the buffer.

This is untested but should steer you in the right direction:

function theme_load_more_posts() {
    check_ajax_referer( 'theme-load-more-posts-nonce', 'nonce' );

    $args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array();
    $args['post_type'] = isset( $args['post_type'] ) ? esc_attr( $args['post_type'] ) : 'post';
    $args['paged'] = esc_attr( $_POST['page'] );
    $args['post_status'] = 'publish';
    $loopContent = '';
    $loop = new WP_Query( $args );
    if( $loop->have_posts() ): 
        while( $loop->have_posts() ): $loop->the_post();
            $loopContent .= get_content_template();
        endwhile; 
    endif;
    ob_start();
    echo do_shortcode($loopContent);
    $data = ob_get_clean();
    wp_send_json_success( $data ); // Performing do_shortcode here but it doesn't work.
    wp_reset_postdata();
    wp_die();
}
add_action( 'wp_ajax_theme_load_more_posts', 'theme_load_more_posts' );
add_action( 'wp_ajax_nopriv_theme_load_more_posts', 'theme_load_more_posts' );
|improve this answer|||||
  • Thank you for your inputs. I tried your suggestion but the shortcodes are still not rendered. :( – JMDee Jun 1 '16 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.