Skip to:

Get last page in bb_init

  • zaerl


    Hi there. I’m writing a small plugin and I need to check if the actual page is the last page of a topic. I’ve seen that this is the standard way:

    if(bb_current_user_can('write_posts') && bb_is_topic())
    global $topic, $page;
    $add = topic_pages_add();
    $last_page = get_page_number( $topic->topic_posts + $add );

    if($last_page == $page) do whatever you want

    but I want to use this code during the bb_init callback. It doesn’t work and after some simple tests I’ve seen that $topic isn’t defined (yet). Is there a way to get last page during this callback?

    Thanks in advance.

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

  • kevinjohngallagher


    Hi Zaerl,

    I’m working on a plugin and have hit a similar issue.

    I need to call certain functions before any data is sent to the browser, but after $forum and $topic etc have been worked out.

    bb_init actions are called before this.

    Did you ever find the action that we can call ?



    Gautam Gupta


    Use bb_topic.php_pre_db action.



    Thank you Gautam!

    EDIT: Actually I’m looking for an action that gets called on every page after the options and database hits have been done, and before any info has been sent to a browser.

    I’ve tried:


    EDIT 2: Actually, fingers crossed bb_index.php_pre_db will work.



    bb_index.php_pre_db didn’t work. It only worked on the homepage.



    Hi Kevin. I found a “solution”. But I’m not proud of my code:

    add_action('bb_topic.php', 'the_function');
    add_action('bb_edit-post.php', 'the_function');
    add_action('bb_forum.php', 'the_function');
    add_action('bb_front-page.php', 'the_function');
    add_action('whatever_other_template_page.php', 'the_function');

    Basically speaking I hook those template pages that I need. The files in the root folder call:

    bb_load_template('template_file.php', some_stuff);

    in the very last line. That function calls the action bb_template_file.php, include() template_file.php and than calls the action bb_after_template_file.php

    Maybe there’s a better solution. I’m not a bbPress expert.



    This is a long time bug with bbpress that I’ve pointed out a few times but it’s too late now I guess to change it’s behavior.

    The problem is that repermalink is not called before the bb_init hook so little is known about the page you are on.

    If you try to call repermalink manually, it may mess up things later because it doesn’t like to be called twice. However in some cases you can get away with it.

    Note it’s almost ALWAYS more efficient to bypass the bbpress API unless the data you need is already loaded in memory/cache.

    You can calculate the last page with a single get_var query, so it’s more efficient to do that yourself if you need it right away and can’t wait for when the templates start loading (or are bypassing the templates loading).

    Since bbpress does not re-calculate post-position when a post is deleted, if there are deleted posts in a topic, the page calculation from the API will almost certainly be wrong (the more deleted, the more likely wrong if the user is not admin). Hence using your own query will again be better.

    Somewhere I have a really fancy mysql query to calculate the real page a post is on, let me know if you want me to dig for it.

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