888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

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.

Post Image The Easy Peasy Way
Wp attachment functions?
26 comments
page 1065
Dont Mess With My Toot Toot
Wordpress custom content?
16 comments
page 599
Post Image The Easy Peasy Way
Inserting an image url in comments?
26 comments
page 1065
Using Your Own Url Shortener
String shortner?
4 comments
page 1190
Quick N Dirty Admin Login Screen
My admin login page?
no comment
page 128
Using Your Own Url Shortener
How to build a shortner link?
4 comments
page 1190
Post Image The Easy Peasy Way
Get post attacments?
26 comments
page 1065
Theming Habari Vs Wordpress
Habari vs wordpress?
13 comments
page 440
Are Child Themes The Best Option
Wp child themes?
15 comments
page 1262
Upload From Url
Upload to url?
6 comments
page 326
Silence Is Golden
Silence is golden wordpress?
3 comments
page 213
How To Add Sidebars To A Theme
Html hot add a side bar?
10 comments
page 1053
Fun With Sidebar Tabs Styling
Position of the tabs thematic?
2 comments
page 336
How To Add Sidebars To A Theme
How to add picture to the wordpress sidebar on sidebarphp?
10 comments
page 1053
Using Your Own Url Shortener
Short url?
4 comments
page 1190
Why I Ditched Disqus
Styling disqus widgets?
5 comments
page 1175
Wordpress 25 Exif Fields
Wordpress exif?
12 comments
page 230
Html 5 Gallery
Html 5 gallery?
6 comments
page 1305
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Dont Mess With My Toot Toot
Fun with toots?
16 comments
page 599
Converting Wordpress Themes To Habari
Habari timthumb?
one comment
page 694
Wordpress 25 Exif Fields
Habari timthumb?
12 comments
page 230
Fun With Sidebar Tabs Styling
Tabs with html css on same page javascript?
2 comments
page 336
Using Your Own Url Shortener
How to have own url short?
4 comments
page 1190
Post Image The Easy Peasy Way
Insert conditional image php wp?
26 comments
page 1065
How To Add Sidebars To A Theme
Wordpress sidebar above main sidebars?
10 comments
page 1053
Using Your Own Url Shortener
Run short url using htaccess?
4 comments
page 1190
Html 5 Gallery
Html5 simple wordpress theme?
6 comments
page 1305
Dont Mess With My Toot Toot
New posttype not displayed wordpress?
16 comments
page 599
Quick N Dirty Replacement Text
Wpupdatepost object?
no comment
page 122
Updating Code Snippets Here
Fun wordpress plugin?
one comment
page 1338
Updating Code Snippets Here
Wordpress fun plugins?
one comment
page 1338
How To Add Sidebars To A Theme
Wp register sidebars?
10 comments
page 1053
  1 query every 2084 seconds, updated 1 seconds ago.
Post a comment?