How to load views do_shortcode in ajax situation

Support MB Views How to load views do_shortcode in ajax situationResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #20310
    daekadogdaekadog
    Participant

    Hi,

    I have set my view to Custom

    I load my content via ajax using this

    $shortcode = '[mb_views id="'.$post->ID.'"]';

    //$shortcode = '[mbv id="'.$post->ID.'"]'; // depends on which is correct see pic below

    echo do_shortcode($shortcode);

    But it only outputs the shortcode

    Also the shortcodes differ for views
    see pic below

    Mb View settings

    How do I display shortcodes for MB Views via ajax?

    Thanks in advance

    Pat

    #20311
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Thanks for pointing this out, the right shortcode is [mbv id="9865"].

    To load the shortcode via ajax, this guide maybe helps you https://wordpress.stackexchange.com/questions/108874/loading-shortcode-with-ajax.

    You can also check the correct shortcode with view ID before passing the variable to the shortcode.

    $post_id = 9865;
    $shortcode = '[mbv id="' . $post_id . '"]';
    echo do_shortcode( $shortcode );
    #20321
    daekadogdaekadog
    Participant

    Hi,

    I have done some tests and can confirm that the method

    $post_id = 9865;
    $shortcode = '[mbv id="' . $post_id . '"]';
    echo do_shortcode( $shortcode );

    Will work on a full page refresh but not if the page is loaded via an ajax method

    Also, I have tested this with other shortcodes in my system using the "echo do_shortcode( $shortcode );" method and they load all via ajax as without problem

    Any ideas?

    #20327
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The shortcode seems does not render in the response of Ajax call, you can try to load the shortcode inside the function wp_localize_script().

    wp_localize_script( 'script-handle', 'pluginObject', array(
        'mbView' => do_shortcode('[mbv id="9865"]')
    ) );

    For more information, please follow this guide https://wordpress.stackexchange.com/questions/121560/do-shortcode-inside-ajax-callback.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.