NEW WORDPRESS TEMPLATES Rotating Header Image

Adding Topics View Counter in BBPress

BBPress is a WordPress Plugin alternative to other Forum Software such as vBulletin, phpBB, Xenforo and Vanilla. Although BBPress did not have as many features like vBulletin and Xenforo, it had a more higher Search Engine Optimize score than both of them plus BBPress is Free to use. BBPress use WordPress Permalink which are Better SEO Friendly, many webmaster are taken interest in using BBPress as their Community Forum Tool.

Today i am going to show you how to add a view counter to BBPress topics.

First, make sure you have BBPress plugin installed, you can download the BBPress WordPress Plugin here.

1. Adding View Counter Function

open your theme functions.php and add this line of code

if( !function_exists('get_wpbbp_post_view') ) :
////////////////////////////////////////////////////////////////////////////////
// get bbpress topic view counter
////////////////////////////////////////////////////////////////////////////////
function get_wpbbp_post_view($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0";
    }
    return $count;
}
function set_wpbbp_post_view($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if( $count == '' ){
        add_post_meta($postID, $count_key, '1');
    } else {
        $new_count = $count + 1;
        update_post_meta($postID, $count_key, $new_count);
    }
}
endif;

What does this code do?
this is the function to add view counter to topic meta

2. Add Hook Action to Each Topics

Again, below the newly added code, add this line of code

if( !function_exists('add_wpbbp_topic_counter') ) :
////////////////////////////////////////////////////////////////////////////////
// init the view counter in bbpress single topic
////////////////////////////////////////////////////////////////////////////////
function add_wpbbp_topic_counter() {
global $wp_query;
$bbp = bbpress();
$active_topic = $bbp-current_topic_id;
set_wpbbp_post_view( $active_topic );
}
add_action('bbp_template_after_single_topic', 'add_wpbbp_topic_counter');
endif;

3. Applying or use the counter in template

You can use this code in any bbpress loop

?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?

in my case, i want to replace the ‘voice count’ with ‘view count’ in BBPress topics loop.
e28c6 voice to view count Adding Topics View Counter in BBPress

using a bbpress child template method, i created a bbpress folder name in my theme. Copy and paste loop-single-topic.php from wp-content/plugins/bbpress/templates/default/bbpress/loop-single-topic.php

then edit mytheme/bbpress/loop-single-topic.php line 82+

?php bbp_topic_voice_count(); ?

with the new code

?php echo get_wpbbp_post_view( bbp_get_topic_id() ); ?

voice counter should had been replace by view counter right now.

Good to go now!

Once everything is done, you should have view counter on all your BBPress topics. Now you can track which topics had the most viewed starting today!

Hope you enjoy the read, more BBPress Tips and Tricks coming this month.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>