Skip to:
Content
Pages
Categories
Search
Top
Bottom

bbp_reply_url not working for large topics


  • alriknijdam
    Participant

    @alriknijdam

    Hi there,

    I’m trying to write a script for displaying the recently active topics in general, and active topics since last visit for users. However, I’m running into a problem with bbp_reply_url();

    It works fine for most topics, but we also got some rather large topics. For those the url is not generated correct: It misses the /page/1234/ part, e.g:
    /forums/topic/kleine-vario-vragen-topic/#post-669703 is wrong.
    It should be /forums/topic/kleine-vario-vragen-topic/page/259/#post-669703

    The same thing happens when I click the reply ID in the reply header, it also generates an url without the /page/1234/ part.

    For those urls missing the /page/1234/ part they redirect me to the first page of the topic, instead of the last page & reply. Can anyone help me solve this?

    Best regards,

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

  • Robin W
    Moderator

    @robin-w

    Much of this is written so that I can come back to it in future, so bear with me!!

    IF YOU JUST WANT THE ANSWER – IGNORE THIS SECTION

    bbp_reply_url() calls bbp_get_reply_url.

    This function does maths by dividing the reply position by the number of replies per page in /includes/replies/template on line 487

    $reply_page = ceil( (int) bbp_get_reply_position( $reply_id, $topic_id ) / (int) bbp_get_replies_per_page() );

    bbp_get_reply_position gets the menu order in line 1742

    $reply_position = get_post_field( 'menu_order', $reply_id );

    Now I’m not sure if this reply position is comma separated, or if it is blank, then the rest of this function is adding a comma. The remainder of then function calls bbp_get_reply_position_raw which is in /includes/replies/functions

    this calls bbp_get_topic_reply_count, and in this function I think we have the issue.

    The function is in /includes/topics/template but in includes/core/filters.php

    we have a hook in line 177 which has

    add_filter( 'bbp_get_topic_reply_count', 'bbp_number_format', 10 );

    ‘bbp_number_format’ by default pouts the 000’s comma separator in.

    so if we remove this filter it should work.

    END OF…..IF YOU JUST WANT THE ANSWER – IGNORE THIS SECTION

    So try this in your functions file

    add_action('plugins_loaded', 'rew_fix_reply_numbers');
    
    function rew_fix_reply_numbers () {
    	remove_filter( 'bbp_get_topic_reply_count',    'bbp_number_format', 10 );
    	add_filter( 'bbp_get_topic_reply_count',    'rew_number_format', 10 );
    }
    
    function rew_number_format( $number = 0, $decimals = false, $dec_point = '.', $thousands_sep = '' ) {
    
    	// If empty, set $number to (int) 0
    	if ( ! is_numeric( $number ) )
    		$number = 0;
    
    	return apply_filters( 'rew_number_format', number_format( $number, $decimals, $dec_point, $thousands_sep ), $number, $decimals, $dec_point, $thousands_sep );
    }

    Let me know either way if this works !!


    Robin W
    Moderator

    @robin-w

    ps if it doesn’t, do you have phpmyadin access?


    alriknijdam
    Participant

    @alriknijdam

    Alright, I tested the function you wrote but unfortunately it’s not doing anything.
    In order to try and help you some more you can now visit some topics.
    For everything you wrote above I now display the values in a table so you can see exactly what’s happening. I also made it possible to run and disable your function (although you won’t see any difference in the values)

    You can browse through the forums/topics to see the values for different topics.

    And yes, I have phpMyAdmin acces.

    Funny but strange fact:
    I’ve imported the topics via csv. The topics with recent activity show a wrong value for the last page. The topics that have not been active after the import do show the correct value (the 7th topic and any topic below that have not been active).
    If any I would have expected it to be the other way around.


    Robin W
    Moderator

    @robin-w

    That’s great, and the info on the site page helped greatly, particularly that bbp_get_topic_reply_count() has a number with the comma in it.

    Replies are stored in the wp_posts table. The code creates an entry for the reply order in ‘menu_order’ where there is none, which is what I hoped my filter was fixing. But if the entry has already been created, then it just uses what is in the database. So when you look at a reply, if it has no entry it creates one and then uses that. So by merely looking at a reply you can alter the database! That may explain your funny but strange fact – just by examining an entry you may be changing what is stored !

    Where the reply appears in the display is held in that table under ‘menu_order’

    so I suspect that for instance reply id 119119 – which I looked at – has a bbp_get_topic_reply_count() value of 1,062.

    Can you check the entry in phpmyadmin

    SELECTmenu_orderFROMwp_postsWHEREID= '119119'

    and see if the entry reads 1,062 or 1062 or 0 !

    That will get us further forward


    alriknijdam
    Participant

    @alriknijdam

    Sorry for the late answer, I missed your last reply because I forgot the ‘notify me’ checkbox.

    I took a look in phpMyAdmin and the menu_order is non of the above, it is 2
    If you visit a topic you can also see that by looking at the $reply_position = get_post_field( 'menu_order', $reply_id ); value 🙂


    alriknijdam
    Participant

    @alriknijdam

    As this is still not resolved I wrote a simple script to solve this issue. For those stumbeling up on this here it is.

    $topic_id		= bbp_get_topic_id();
    $reply_id		= bbp_get_reply_id();
    $position_raw	= bbp_get_reply_position($reply_id, $topic_id);
    // Remove the comma
    $position		= str_replace(',', '', $position_raw);
    // Devide by the number of posts per page, in my case 15
    // I didn't test it but I believe you can also use: get_option( 'posts_per_page' );
    $page_raw		= ceil($position / 15 );
    if ($page_raw > 1) $page = '/page/' . $page_raw;
    else $page 		= '';
    $slug			= get_post_field('post_name', get_post($topic_id));
    // Replace YOUR_WEBSITE_URL with your own or use: bloginfo('wpurl')
    // to retrieve it, but don't forget to concenate
    $correct_link	= 'YOUR_WEBSITE_URL/forums/topic/' . $slug . $page . '/#post-' . $reply_id;

    You can probably place this inside some function but I placed it inside loop-single-reply.php and placed that inside my child-theme. In that case you also have to replace this:

    <a href="<?php echo bbp_reply_url(); ?>" class="bbp-reply-permalink">#<?php echo bbp_reply_id(); ?></a>

    with:

    <a href="<?php echo $correct_link; ?>" class="bbp-reply-permalink">#<?php echo $reply_id; ?></a>


    codestars
    Participant

    @codestars

    Thank you alriknijdam, this was exactly what I needed.

    I improved your code a bit, for example you can use bbp_get_topics_per_page() so the amount of replies per page will always be correct. And you can use bbp_get_root_slug() to dynamically get the root URL of where your forum is located.

    /**
     * Get reply in topic permalink.
     *
     * @param int $reply_id
     * @param int $topic_id
     *
     * @return string
     */
    function custom_get_reply_in_topic_permalink( $reply_id, $topic_id ) {
    
    	$position_raw = bbp_get_reply_position( $reply_id, $topic_id );
    	$position     = str_replace( ',', '', $position_raw );
    	$page_raw     = ceil( $position / bbp_get_topics_per_page() );
    	$page         = $page_raw > 1 ? '/page/' . $page_raw : '';
    
    	$slug = get_post_field( 'post_name', get_post( $topic_id ) );
    
    	return bbp_get_root_slug() . '/topic/' . $slug . $page . '/#post-' . $reply_id;
    }

    Can be used like this:
    <a href="<?php echo custom_get_reply_in_topic_permalink( $reply_id, $topic_id ); ?>">

    Enjoy!

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