Skip to:
Content
Pages
Categories
Search
Top
Bottom

Can AJAX be enabled for bbPress?


  • berry metal
    Participant

    @erikalleman

    Is the bbPress favorite and subscribe button AJAX?

    On some forums they confirmed that even posting is not AJAX anymore.
    How do I enable AJAX for my bbPress?

    For me both the favorite and the subscribe button reloads the page.
    I can see that on this site the favorite button works with AJAX, so why isn’t AJAX then in the bbPress plugin?

Viewing 10 replies - 1 through 10 (of 10 total)

  • John James Jacoby
    Keymaster

    @johnjamesjacoby

    Probably an issue with a plugin conflict or theme.

    Standard troubleshooting rules apply. Deactivate one plugin at a time and switch to a WordPress Twenty Something theme until it starts working, then work backward until you identify what’s causing it.


    berry metal
    Participant

    @erikalleman

    Thanks!


    berry metal
    Participant

    @erikalleman

    The reason is hard for me to debug this issue is that I am using this code to output a bbpress topic under each post that has the same name as the current post title:

    add_shortcode( 'bbpresscomments', function() {
        $output     = '';
        $current_id = function_exists( 'vcex_get_the_ID' ) ? vcex_get_the_ID() : get_the_ID();
        $topics = new WP_Query( array(
            'post_type' => 'topic',
            'posts_per_page' => -1,
            'fields' => 'ids',
        ) );
        if ( $topics->have_posts() ) {
            foreach( $topics->posts as $topic ) {
                if ( get_the_title( $topic ) == get_the_title( $current_id ) ) {
                    $output .= do_shortcode( '[bbp-single-topic id="' . intval( $topic ) . '"]' );
                }
            }
        }
        return $output;
    } );

    This code is theme specific, so testing with an other theme doesn’t make sense in this case.
    I will open now a ticket to my theme developer, but if you can see anything in this code that would make AJAX not work, please let me know.

    Thanks.


    berry metal
    Participant

    @erikalleman

    Hi again,

    I added this code:

    if ( function_exists( 'bbp_enqueue_script' ) && function_exists( 'bbp_get_version' ) ) {
        bbp_enqueue_script( 'bbpress-engagements', 'js/engagements.js', array( 'jquery' ), bbp_get_version(), true );
    }

    so now it looks like this:

    add_shortcode( 'bbpresscomments', function() {
        $output     = '';
        $current_id = function_exists( 'vcex_get_the_ID' ) ? vcex_get_the_ID() : get_the_ID();
        $topics = new WP_Query( array(
            'post_type' => 'topic',
            'posts_per_page' => -1,
            'fields' => 'ids',
        ) );
        if ( $topics->have_posts() ) {
            // enqueue scripts on this next line...
            if ( function_exists( 'bbp_enqueue_script' ) && function_exists( 'bbp_get_version' ) ) {
        bbp_enqueue_script( 'bbpress-engagements', 'js/engagements.js', array( 'jquery' ), bbp_get_version(), true );
    }
            foreach( $topics->posts as $topic ) {
                if ( get_the_title( $topic ) == get_the_title( $current_id ) ) {
                    $output .= do_shortcode( '[bbp-single-topic id="' . intval( $topic ) . '"]' );
                }
            }
        }
        return $output;
    } );

    because I can see that code that enables all functionality including AJAX, is only enqueued for bbPress pages:

    https://github.com/bbpress/bbPress/blob/afa6030c3e27c4fe582743074a603d25b5e4de48/src/templates/default/bbpress-functions.php#L151

    And it’s not enabled for bbPress shortcodes.

    Is this right?

    Could you please tell me what is wrong with my code, and why cannot I enqueue the scripts that will enable AJAX for the shortcodes?

    Instead of proper enqueuing, my code makes the buttons not work at all, when I click on them, nothing happens.


    berry metal
    Participant

    @erikalleman

    Oh and I forgot to mention that I did test it by deactivating plugins and changing theme. The issue persists, when the shortcode is used.
    Is AJAX enabled for the buttons if the Subscribe and Favorite buttons are used within the Replies shortcode?


    mrentropy
    Participant

    @mrentropy

    Hi Erika,

    Were you ever able to make the AJAX buttons work? I’m having a similar experience of the page always reloading after favoriting or subscribing.


    mrentropy
    Participant

    @mrentropy

    Actually, in my case, the issue was that I was overriding the theme files and had removed this span. <span class="bbp-row-actions">. bbPress must try ajax first, and if it fails, just redirect the page — and the JavaScript logic must look for that class name? Adding the span back, made the page behave as desired.


    berry metal
    Participant

    @erikalleman

    Hi @mrentropy,
    I stopped using bbpress (for other reasons) a long time ago and even my website,
    so I cannot try out your solution at the moment.
    Thanks for the feedback.
    No, I could not solve the issue.


    mrentropy
    Participant

    @mrentropy

    @erikalleman thanks so much for taking the time to respond — especially when it’s not currently something you are working on.

    I know you will not need this, but I thought in case others have this issue, and come across this post, I’d add that there is more needed to solve this issue than I posted last week.

    For both of us, we were/are attempting to utilize the ajax behavior for a series of topics. However, the JavaScript that bbPress utilizes targets an ID and not a class name. This means, that outputting a series of topics with Favorite links will all have the same ID, and only the first topic will use ajax.

    My solution ended up requiring template overrides for loop-single-topic.php to create unique IDs for every favorite link. Then I wrote a new click function for Favorites, as well as a new bbp_ajax_call function in js/engagements.js.


    berry metal
    Participant

    @erikalleman

    Noted.
    For the case I will need a forum software again.
    Thanks.

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.
Skip to toolbar