Skip to:

Re: Plugin: bb-Topic-Views

All I did was split display_view_count() into two functions, display_view_count() and get_view_count(). If for example you wanted the views in a seperate column, you’d add the column HTML and then add

<?php $view_count = get_view_count(); echo $view_count; ?>

This is what I replaced display_view_count( $title ) with, here for Smurf’s convenience. You’re about to get mail.

Oh, before I forget – hopefully now that plugins need activation in 1.0-alpha, there’s also an activation hook or the like. That would let you do a lot of preparation first, making the code much easier. :)

Edit: there is! bb_activate_plugin_XXX. Maybe you can use that – if you want to and figure it out, tell us/make an entry on the wiki. :)

function display_view_count ($title)
global $bbdb, $topic;
$topic_id = $topic->topic_id;

$view_count = get_view_count( $topic_id );

//Builds the text to be appended to the title
$count_display = " <em>($view_count views)</em>"; //This sets the view count var to the existing number of views, if greater than 0
//Makes this plugin play nice with the Page Links plugin by putting the pages (if they exist) on a new line
if (function_exists('page_links_add_links')) {
$count_display .= "n";
$title .= $count_display;

return $title;

function get_view_count( $topic_id )
$view_count = $bbdb->get_var("SELECT meta_value FROM $bbdb->topicmeta WHERE topic_id = $topic_id AND meta_key='views'");

if ( $view_count <= 0 ) {
//If the view count hasn't been initialized...
if ($topic->topic_posts >= 1) {
/* Sets the new record to the number of posts that have been made in a topic */
$view_count = $topic->topic_posts; //sets the view_count number
} else {
$view_count = 0; //can't think of a time when this would be necessary (phantom topics???)

//Adds the record to the DB so it isn't zero any longer
$bbdb->query("INSERT INTO $bbdb->topicmeta ( meta_id, topic_id, meta_key, meta_value) VALUES ( NULL , $topic_id, 'views', $view_count )");

return $view_count;

Skip to toolbar