Quick n’ Dirty Comment Stats

This is plugin number 7 in my Quick N Dirty plugin series and things are now starting to get a little more complex, and a little more interesting. In this post I will demonstrate an admin page for displaying stats.
Creating an admin page is actually a two stage process involving one hook and one function. We also need a function to actually output the content of the page. This means there are two functions this time, not three.
This hook this plugin uses is an action called: admin_menu. This is called when the admin menu is being constructed and that lets us run commands to add pages to the menu.
This where the first function comes in. As with all the earlier plugins the hook calls a function. This function adds a link to the admin menu. The admin menu link will contain parameters that tell the admin system to load this plugin file, and specifically, to run our second function. The second function contains all the logic and content for the actual page.
Effectively we have a chain: Action -> Function 1 -> Function 2
This plugin might look more complicated than the others but most of the complexity comes from the fact that I have used Google Charts to display the data.
If you were happy to simply print out the data, or create a series of div tags with a length set according to the value then it may be a little easier but the options available and the quality produced by Google Charts makes it worthwhile in my opinion. I won’t be explaining in detail how Google Charts works. The plugin puts together a list of variables in a URL and uses that as the src attribute for a normal image. If you want to look more into what the various parts of the Google Charts URL you can find the details on the Google Charts API Page.
<?php /* Plugin Name: Quick n’ Dirty Comment Stats Plugin URI: http://www.wp-fun.co.uk/2008/01/27/quick-n-dirty-comment-stats/ Description: Adds a statistics page to the comments section and displays some basic information Author: Andrew Rickmann Version: 1 Author URI: http://www.wp-fun.co.uk */ //This is the line that adds your filter into the list. // ‘admin_menu’ is the name of the filter // ‘qnd_create_comment_stats_menu’ is the name of the function, below add_filter( ‘admin_menu’ , ‘qnd_create_comment_stats_menu’ ); function qnd_create_comment_stats_menu(){ //this function only calls one comment, the comment that creates the submenu page add_submenu_page( ‘edit-comments.php’ , //the parent page that the submenu will be below ‘Quick n\’ Dirty Comment Stats’ , //the page title ‘Stats’ , //the title displayed on the menu 8 , //the security level 0 = lowest 10 = highest __FILE__ , //the file where the page can be found (this one) ‘qnd_output_comment_stats_page’ ); // the function that outputs the page (below) } function qnd_output_comment_stats_page(){ //do any processing that must be done before the page is loaded //get information here, or process post variables submitted from a form //start to put together the url needed for google charts $chart_base_uri = ‘http://chart.apis.google.com/chart?chs=650×200&cht=bvg&chxt=x,y’; $chart_posts_addition = ‘&chtt=Comments%20on%20the%20last%20twenty%20posts’; //get posts, only using the last 20 for example purposes $posts = get_posts(‘numberposts=20&orderby=post_date&order=ASC’); //create an array to hold the number of comments and post ID $comment_array = array(); $post_title_array = array(); //assign each value to the requesite array foreach($posts as $post){ $comment_array[] = (int) $post->comment_count; $post_id_array[] = (string) $post->ID; } //change the scale to use the full height of the graph $maxca = max( $comment_array ); $scale = 100 / $maxca; //apply to scale to each comment count for($i = 0 ; $i < count($comment_array) ; $i++ ){ $comment_array[$i] = $comment_array[$i] * $scale; } //update the URL to contain the appropriate range of values (y axis) $chart_posts_addition .= ‘&chxr=’ . ‘1,0,’ . $maxca; //add the values, and the label for the x axis to the URL $chart_posts_addition .= ‘&chd=t:’ . join( ‘,’ , $comment_array ) ; $chart_posts_addition .= ‘&chxl=0:|’.join( ‘|’ , $post_id_array ) ; //use the string to generate a google chart url //output the page itself ?> <div class="wrap"> <h2>Quick n‘ Dirty Comment Stats</h2> <img src="<?php echo $chart_base_uri . $chart_posts_addition;?>" alt="" /> </div> <?php } ?>
Here is an image showing the result on my test system:

I hope most the comments on the plugin above do most of the explaining, if they don’t, or you have any related questions just throw in a comment below and I will add to the description.
I think the important point to note is that most of the code involves changing the data so that it will meet the requirements for the Google Charts URL. The code that actually creates the page is simple, there is one command that adds the link to the menu and one hook as in the rest of the plugins so I hope you can see that creating admin pages is actually very easy.
If you feel the desire to expand this you might want to consider a pie chart showing how your posts are divided amongst your categories or a chart showing which pages were most commented on over the past month.
Tomorrow I’m going to demonstrate an antispam technique that, so far has not let a single spambot comment through on my own blog (so far, touch wood).
Note: If you copy the content of this plugin you will need to replace all the quote marks as WordPress replaces them with fancy ones.
Comments
Leave a Reply
I am currently testing a comment link policy which means commenters do not get a link. There is a poll, and open comments for feedback on the comment policy page.
