Adding Topics View Counter in BBPress

BBPress is a WordPress Plugin choice to other Forum Software such as vBulletin, phpBB, Xenforo and Vanilla. Although BBPress did not have as many facilities like vBulletin and Xenforo, it had a some-more aloft Search Engine Optimize score than both of them and BBPress is Free to use. BBPress use WordPress Permalink that are Better SEO Friendly, many webmaster are taken seductiveness in regulating BBPress as their Community Forum Tool.

Today i am going to uncover we how to supplement a view counter to BBPress topics.

First, make certain we have BBPress plugin installed, we can download a BBPress WordPress Plugin here.

1. Adding View Counter Function

open your thesis functions.php and supplement this line of code

if( !function_exists('get_wpbbp_post_view') ) :
////////////////////////////////////////////////////////////////////////////////
// get bbpress subject perspective 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');
        lapse "0";
    }
    lapse $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 formula do?
this is a duty to supplement perspective opposite to subject meta

2. Add Hook Action to Each Topics

Again, next a newly combined code, supplement this line of code

if( !function_exists('add_wpbbp_topic_counter') ) :
////////////////////////////////////////////////////////////////////////////////
// init a perspective opposite in bbpress singular 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 a opposite in template

You can use this formula in any bbpress loop

?php relate get_wpbbp_post_view( bbp_get_topic_id() ); ?

in my case, i wish to reinstate a ‘voice count’ with ‘view count’ in BBPress topics loop.
voice-to-view-count

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

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

?php bbp_topic_voice_count(); ?

with a new code

?php relate get_wpbbp_post_view( bbp_get_topic_id() ); ?

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

Good to go now!

Once all is done, we should have perspective opposite on all your BBPress topics. Now we can lane that topics had a many noticed starting today!

Hope we suffer a read, some-more BBPress Tips and Tricks entrance 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=""> <s> <strike> <strong>