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.

Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Using Your Own Url Shortener
Rewriterule shorten url?
4 comments
page 1190
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
My Experience Of Flexx
How to edit flexx theme wordpress?
4 comments
page 1026
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Post Image The Easy Peasy Way
List image attachments wordpress?
26 comments
page 1065
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp image attached linkable?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp get first image for post?
26 comments
page 1065
Updating Code Snippets Here
Cacheaboutblank?
no comment
page 1338
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Simple login screen css?
no comment
page 128
Six Million Ways To Die Choose One
One million ways to die?
14 comments
page 1128
Updating Code Snippets Here
Interesting wordpress websites?
no comment
page 1338
Post Image The Easy Peasy Way
Attachment images in wordpress single posts?
26 comments
page 1065
Six Million Ways To Die Choose One
Is there an easy way to die?
14 comments
page 1128
Quick N Dirty Replacement Text
Addaction replacement?
no comment
page 122
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Are Child Themes The Best Option
Thesis child theme?
15 comments
page 1262
Charcoal Theme Available For Wordpress
Charcoal wordpress site admin?
2 comments
page 959
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
How To Add Sidebars To A Theme
Register sidebar in functionphp?
10 comments
page 1053
Why I Ditched Disqus
Disqus themes?
5 comments
page 1175
Post Image The Easy Peasy Way
How to use wp get attachment url?
26 comments
page 1065
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
What A Body
Whatabodybiz?
11 comments
page 1126
Html 5 Gallery
Html5 chat?
6 comments
page 1305
Adding Settings To Admin Pages
Admin add field wordpress?
3 comments
page 793
Wordpress Chat
Wp chat page?
3 comments
page 1308
Html 5 Gallery
Html5 and tabs?
6 comments
page 1305
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
Creating Custom Urls
Custom url rewrite wordpress?
6 comments
page 80
  1 query every 1559 seconds, updated 1 seconds ago.
Post a comment?