Create A Simple Blog Stats Plugin

Tutorials | By | 11 October 2010 | Comments 7

Developing plugins for WordPress at a glance might well strike you as a daunting task on the face of it, and many people don’t bother delving any deeper, truth be known the task isn’t that painful at all.

I have written a really simple plugin for the readers of WPZine and will take it apart to show you how a plugin functions, so you can add to the plugin, or even use it as a guide for creating your own in the future.

I decided to write something nice and simple so the plugin is called “WP Blog Stats” and it will display counts for Total Posts, Total Comments, Draft Posts and Total Trackbacks in a widget.

Now the plugin might not be very useful to most of you in some respects, but the idea behind it is to look into plugin development at a very basic level.

OK lets get started, using your favorite code editor, create a new page and paste in the following, the save as: “wpstats.php”

<?php
/*
Plugin Name: WP Blog Stats
Plugin URI: http://wpzine.com
Description: Stats plugin that shows counts of various items on your blog.
Author: Paul Maloney
Version: 1.1
Author URI: http://paulmaloney.net
*/
?>

This is the plugin meta, it enables WordPress to determine the plugins name and description etc..

We will then need to tell the plugin what we would like it to actually do, so we create a function:

function wpstats()
{
// This connects the plugin to the database
global $wpdb;

// this section gets the post count
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

// this section gets the comment count
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'"); if (0 < $numcomms)
$numcomms = number_format($numcomms);

// this section gets the trackback count
$track = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = 'pingback' OR comment_type = 'trackback'"); if (0 < $track)
$track = number_format($track);

// this section gets the draft post count
$numdrafts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft'");
if (0 < $numdrafts) $numdrafts = number_format($numdrafts);

// here is some simple styling
echo "<style>
.wpstats {margin: 10px 0 10px 0;}
.wpstats p{font-weight:bold; margin: 0 0 5px 0;}
</style>";

// this part makes the counts display
echo "<div class=\"wpstats\">";
echo "<p>Total Posts - $numposts</p>";
echo "<p>Total Comments - $numcomms</p>";
echo "<p>Total Trackback - $track</p>";
echo "<p>Draft Posts - $numdrafts</p>";
echo "</div>";
}

I have commented the duty of each of the sections above, in this example we have placed the CSS inside the plugin to keep it in one file. You can of course remove or edit the styling.

There is a few pieces to add to enable the plugin to function correctly, the function below tells the plugin what title to use in your sidebar:

function widget_mystats($args) {
extract($args);
echo $before_widget;

// the title that will be displayed above your widget
echo $before_title;?>Blog Stats<?php echo $after_title;

wpstats();
echo $after_widget;
}

And finally this will tell the plugin to register it as a widget:

function mystats_init()
{
register_sidebar_widget(__('WP Stats'), 'widget_mystats');
}
add_action("plugins_loaded", "mystats_init");

This is what the plugin looks like when completed:

<?php
/*
Plugin Name: WP Blog Stats
Plugin URI: http://wpzine.com
Description: Stats plugin that shows counts of various items on your blog.
Author: Paul Maloney
Version: 1.1
Author URI: http://paulmaloney.net
*/

function wpstats()
{
// This connects the plugin to the database
global $wpdb;

// this section gets the post count
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

// this section gets the comment count
$numcomms = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'"); if (0 < $numcomms)
$numcomms = number_format($numcomms);

// this section gets the trackback count
$track = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_type = 'pingback' OR comment_type = 'trackback'"); if (0 < $track)
$track = number_format($track);

// this section gets the draft post count
$numdrafts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'draft'");
if (0 < $numdrafts) $numdrafts = number_format($numdrafts);

// here is some simple styling
echo "<style>
.wpstats {margin: 10px 0 10px 0;}
.wpstats p{font-weight:bold; margin: 0 0 5px 0;}
</style>";

// this part makes the counts display
echo "<div class=\"wpstats\">";
echo "<p>Total Posts - $numposts</p>";
echo "<p>Total Comments - $numcomms</p>";
echo "<p>Total Trackback - $track</p>";
echo "<p>Draft Posts - $numdrafts</p>";
echo "</div>";
}

function widget_mystats($args) {
extract($args);
echo $before_widget;

// the title that will be displayed above your widget
echo $before_title;?>Blog Stats<?php echo $after_title;

wpstats();
echo $after_widget;
}

function mystats_init()
{
register_sidebar_widget(__('WP Stats'), 'widget_mystats');
}
add_action("plugins_loaded", "mystats_init");

?>

To test it out, upload to your blogs plugin directory then visit wp-admin and enable in the plugins menu. You can then visit the widgets menu and drag it over to your sidebar widget box.

Visit your blog and it should then appear in your sidebar and display your blog stats!

Paul Maloney is a UK based web designer/developer and runs the design agency Tropica. He particularly enjoys using and working with WordPress and has a keen interest in typography. You can also follow Paul on Twitter.

Visit Paul Maloney's website

7 Comments

  • October 12, 2010
  • October 12, 2010
  • October 12, 2010
  • October 21, 2010
  • November 2, 2010
  • November 3, 2010

Leave A Comment.