bbPress

Simple, fast, elegant

bbPress Plugin Browser »

bb Topic Views (1.6.4)

Download

Version: 1.6.4

Last Updated: 2009-2-6

Requires bbPress Version: 0.8 or higher

Compatible up to: 0.9

Author Homepage »

Plugin Homepage »

Average Rating

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

Your Rating

Authors _ck_, wittmania

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. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  2. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  3. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  4. this is great! very cool, definitely using it. thanks :)

    Posted: 2 years ago #
  5. so complicated instalation

    Posted: 2 years ago #
  6. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  7. _ck_, you are great.

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

    Posted: 2 years 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: 2 years 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: 2 years ago #
  10. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  11. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 2 years ago #
  12. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    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: 1 year ago #
  13. csseur3

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Member

    hello,

    i have this error in my frontpage: Database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND meta_key='views'' at line 1]
    SELECT meta_value FROM bb_topicmeta WHERE topic_id = AND meta_key='views'
    Caller: get_view_count

    Database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'views', )' at line 1]
    INSERT INTO bb_topicmeta ( meta_id, topic_id, meta_key, meta_value) VALUES ( NULL , , 'views', )
    Caller: initialize_view_count

    why? :)

    Posted: 1 year ago #
  14. csseur3

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Member

    for information, the code in my front-page is <td class="num"><?php show_view_count(); ?></td> any idea?

    bye

    Posted: 1 year ago #
  15. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    csseur3 are you on a windows or linux server

    Posted: 1 year ago #
  16. csseur3

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Member

    gnu/linux ubuntu server 8.04 :p

    Posted: 1 year ago #
  17. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    That's a really strange error.
    It should have "0" set for the topic_id and there is absolutely nothing in the view variable.

    Maybe try deactivating/reactivating it again - though that's not really an answer since it doesnt have it's own tables. I suspect sessions are failing for some reason.

    Posted: 1 year ago #
  18. csseur3

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Member

    so, in the latest discussions column, the number of views is show and correct.

    i use this plugin with your my-views plugin, ck.

    bye

    Posted: 1 year ago #
  19. _ck_

    5 stars
    4 stars
    3 stars
    2 stars
    1 star

    Moderator

    This plugin (version 1.5) will not work with bbPress 1.0 because of it's direct query to $bbdb->topicmeta. I will look into releasing a modified version since the original author seems to have abandoned it. "My Views" will also have to be modified for bbPress 1.0 support.

    Posted: 1 year ago #

RSS feed for this topic

Add a Comment »

You must log in to post.

Code is Poetry.