bbPress

Simple, Fast, Elegant

bbPress plugin browser »

bb-Topic-Views (1.5)

Download

Version: 1.5

Last Updated: 2007-5-31

Author Homepage »

Plugin Homepage »

Average Rating

5 stars
4 stars
3 stars
2 stars
1 star
(5)

Your Rating

Author: Mike Wittmann

bb-Topic-Views keeps track of how many times each topic has been viewed, and then displays the count alongside the title of the topic on the front page, on forums pages, and on tags pages.

The plugin is written in such a way that it does not double-count views when a visitor browses to a different page in the same topic. If no view count record exists for a specific topic, the plugin will create a record for it. Rather than setting the initial view count to zero, the plugin sets it to the number of posts in the topic, because it has obviously been viewed at least as many times as people have posted in it! This is especially nice for adding the plugin to existing bbpress forums so the view count isn't zero for every single topic.


  1. This plugin seems very well written
    and documented excellently. Thank you!

    ideas/questions:
    1. I am exploring how to gather the total views for an individual forum...

    2. could most viewed be turned into a "view"
    ie. /forums/view/most-viewed

    Posted: 11 months ago #
  2. Here's a plugin to extend the topic-views plugin and add a "most-views" view page (ie. view.php?view=most-views)

    It does it with a minimum number of mysql calls, just 10 to display an entire page on my setup. You have to hack your views.php template to add the views column, use $topic->views for the view count to use the cached data in memory instead of an extra mysql query for each topic.

    function most_views_views( $views ) {
    	global $views;
    	$views['most-views'] = 'Topics with the most views';
    	return $views;
    }
    add_filter('bb_views', 'most_views_views');
    
    function most_views( $view ) {
    global $bbdb, $topics, $view_count;
    switch ( $view ) :
    case 'most-views' :
    $limit = bb_get_option('page_topics');
    $where = apply_filters('get_latest_topics_where','');
    $most_views = $bbdb->get_results("SELECT topic_id FROM $bbdb->topicmeta WHERE meta_key='views' ORDER BY cast(meta_value as UNSIGNED) DESC LIMIT $limit");
    foreach (array_keys($most_views) as $i) {$trans[$most_views[$i]->topic_id] =& $most_views[$i];} $ids = join(',', array_keys($trans));
    $topics ="SELECT * FROM $bbdb->topics WHERE topic_id IN ($ids) $where ORDER BY FIELD(topic_id, $ids)";
    $topics = $bbdb->get_results($topics);
    $view_count  = count($topics);
    $topics = bb_append_meta( $topics, 'topic' );
    	break;
    default :
    	do_action( 'bb_custom_view', $view );
    endswitch;
    }
    add_action( 'bb_custom_view', 'most_views' );
    Posted: 11 months ago #
  3. I've now come up with an extremely easy/fast way to grab and display the views per forum in the forum list on the front page (or forums with sub-forums.

    Here's the plugin:

    function forums_views_append($forums) {
    global $bbdb; $sum_meta_value="SUM(meta_value)";
    $forums_views = $bbdb->get_results(" SELECT $sum_meta_value,forum_id FROM $bbdb->topicmeta LEFT JOIN $bbdb->topics ON $bbdb->topicmeta.topic_id = $bbdb->topics.topic_id  WHERE $bbdb->topicmeta.meta_key='views'  GROUP BY $bbdb->topics.forum_id");
    foreach ($forums_views as $forum_views) {$forums[$forum_views->forum_id]->views=$forum_views->$sum_meta_value; }
    return $forums;
    }
    add_filter('get_forums','forums_views_append');

    To display the views, edit your front-page.php template and insert a views header and views column like so:

    <th><?php _e('Views'); ?></th>
    	<th><?php _e('Topics'); ?></th>
    	<th><?php _e('Posts'); ?></th>

    .

    <td><?php echo $forum->views; ?></td>
    	<td><?php forum_topics(); ?></td>
    	<td><?php forum_posts(); ?></td>

    Since there is no clean way to store forum meta data right now in bbpress (apparently an oversight) this will run on every time the forums list is displayed. Maybe not such a good idea for very busy forums, even though it's a single mysql query (nifty eh?).

    Eventually I'll figure out a nice way to store this and only update upon new posts or new topics to reduce overhead (or a cron job perhaps).

    Posted: 11 months ago #
  4. this is great! very cool, definitely using it. thanks :)

    Posted: 11 months ago #
  5. so complicated instalation

    Posted: 11 months ago #
  6. I've now bundled my extensions for this plugin into this new plugin:
    http://ckon.wordpress.com/2007/07/30/new-plugin-my-views-for-bbpress/

    Posted: 11 months ago #
  7. _ck_, you are great.

    The sum() method is so simple... hadn't thought of that one.

    Posted: 11 months ago #
  8. I'm using this very nice plugin.

    After some extensive theme changes and host re-configuring, my views seem "stuck".

    http://appleswitcher.com/wp/forum/

    Any ideas on how to troubleshoot this?

    Posted: 8 months ago #
  9. ruilouis

    Inactive

    Tks for this plugin, it works great.

    I have some questions though,

    I feel like the topic view count won't be incremeted each time I see a topic, but each time I see a different topic. I mean, if i click 2 time on a row on the same topic, i won't upgrade it ? Correct me if I'm wrong?

    Posted: 5 months ago #
  10. I've discovered the way this plugin initiates sessions is not robust enough. Here's are some minor but helpful improvements:

    function views_session_check ()
    {
    	if( !isset( $_SESSION ) && is_topic()) {		// only start session if not already stared and it's a topic page
    		@session_cache_limiter('public');		// allows back button to work without losing form data
    		@session_start();
    	}
    }

    Since Mike seems to be absent these days, I'll attempt to give support for this plugin if anyone needs help. It's hard to track questions here so if you don't get an answer here, also try me on my forum (bbshowcase.org).

    intellivision - are you still having trouble with counted views? There might be a problem with session support on your server or perhaps the meta has corrupted somehow...

    ruilouis - your understanding of the behaviour is correct - if you view the same topic 5 times in a row without looking elsewhere, it won't increase the count - this is a feature as designed, to prevent reading additional pages within the same topic from increasing the count.

    There might be way to keep this plugin working correctly without sessions which would make it work better and more compatible with other plugins/servers. I'll have to look into it sometime.

    Posted: 4 months ago #
  11. I have found that in my above code
    @session_cache_limiter('public');

    is a very bad idea as you'll get expired pages when you refresh in your browser, so skip that part.

    Checking for is_topic however is still a good idea to keep other pages a little faster.

    Posted: 4 months ago #
  12. This plugin uses the php "shorttag" of <? instead of <?php which breaks some PHP on some servers (especially windows). So until the author is contacted and can update it, if you find you cannot activate the plugin, search and replace <? with <?php and it should then work.

    Posted: 2 months ago #

RSS feed for this topic

Add a Comment

You must log in to post.

Code is Poetry.