January 30, 2008

Quick N Dirty

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=650x200&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
?>

Quick n' Dirty Comment Stats

}

?>

Here is an image showing the result on my test system:

Quick n dirty comment stats

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.



Wordpress title showing space?
no comment on page 1371

Wordpress fun?
one comment on page 1376

Live blogging plugin?
4 comments on page 1258

Wordpress 3 admin speed up?
4 comments on page 1321

Framework photoshop?
3 comments on page 296

Fun wp plugins?
one comment on page 1376

Habari vs wordpress?
12 comments on page 440

Wp tags vs categories?
12 comments on page 7

Wordpress rss seo?
one comment on page 1361

Photo albums html5?
6 comments on page 1305

Wordpress chat?
no comment on page 1308

Wordpress exif data?
12 comments on page 230

Css sidear tab?
2 comments on page 336

Wordpress theme html5 blueprint?
6 comments on page 1305

Wordpress shortcode in plugin?
no comment on page 236

Html 50 photo album?
6 comments on page 1305

Get the post attachement?
24 comments on page 1065

Wordpress plugin development 30?
one comment on page 1373

Wordpress plugin development 30?
one comment on page 1373

Disqus formatting?
7 comments on page 1175

Html5 photoalbum?
6 comments on page 1305

Html5 photoalbum?
6 comments on page 1305

Wordpress fun?
one comment on page 1376

Fun wordpress plugins?
one comment on page 1376

Url shortener ideas?
4 comments on page 1190

Url shortener ideas?
4 comments on page 1190

Html 5 photo gallery?
6 comments on page 1305

Multiple post navigation?
no comment on page 1147

Html5 photo galleries?
6 comments on page 1305

Adding images to a wordpress 3 post?
24 comments on page 1065

Html5 photo gallery code?
6 comments on page 1305

Wordpress multiple blog master?
one comment on page 1376

Wordpress 3 tableprefix?
one comment on page 1376

Wordpress 3 tableprefix?
2 comments on page 1374

Using wordpress as a framework?
2 comments on page 335

Single post image size?
24 comments on page 1065

Get featured image src wordpress?
24 comments on page 1065

Disqus wordpress mu?
7 comments on page 1175

Image gallery html 5?
6 comments on page 1305

Wordpress theimage?
24 comments on page 1065

Wpgetattachmentimagesrc size?
24 comments on page 1065
  every 1736s, 1s ago, in 0.02s.
Post a comment?

0s