There are two stats that I love to check for my web properties: the number of backlinks I have, and the Alexa rank of my site. Obviously, the number of backlinks that a webpage has is a large determinant of its rankings on search engines, and I love the Alexa rank tool, because it gives me a nice statistic to measure my progress. So I find myself going to Yahoo’s site-explorer tool a lot, and checking the Alexa tool on my Chrome browser. Since I check these stats as much as I visit my dashboard, however, I thought that it would be useful to display this information on my dashboard.

The problem was that I had never written a WordPress Plugin before.

So, I went through a couple of tutorials and examples of plugins, and I gradually┬álearned the process. The plugin, in all its glory, is now hosted here on this blog, under “Downloads”->”Wordpress Plugins” in the navigation bar above. So let’s get started then on this tutorial.

First, I created a file with the name of my plugin. I called it “monitor-essentials.php”, and in that file I wrote the basic information that WordPress will display to users of the plugin:

<?php
/*
Plugin Name: Monitor SEO Essentials
Description: Allows the user to monitor Yahoo backlinks, and Alexa rank.
Author: Graeme Boy
Author URI: http://pythoughts.com
Plugin URI: http://pythoughts.com/monitor-essentials
Version: 1
*/

So this above is the code that you need to give to WordPress for them to identify your plugin. So far, so good. I already feel like I’m on the path to creating something cool. Next, I created a nice little class, like any other program would have:

class MonitorEssentials {
function MonitorEssentials () {
add_action('wp_dashboard_setup', array($this, 'exe_dashboard_widget'));
}
function exe_dashboard_widget()
{
wp_add_dashboard_widget('me_dashboard_widget', 'Monitor SEO Essentials', array($this,'show_dashboard_widget'));
}
function show_dashboard_widget()
{
?>

The “add_action” function tells WordPress that they need to add something to the dashboard when the dashboard is loaded, and that what needs to be added is contained in the function “exe_dashboard_widget”. Exe_dashboard_widget() then tells it to add a widget with what is contained in “show_dashboard_widget”, and use “Monitor SEO Essentials” as the title. So far, so good. It seems a bit tedious to have so many functions, but I guess this is all understandable.

So simply enough, now we just need to add whatever we want to show in the dashboard to “show_dashboard_widget()” – which is going to be the php and html that we want to show there. If you don’t want to go ahead and make an SEO monitoring widget like mine, you could simply put your favorite quote there, to inspire you whenever you log into your dashboard. :) This is what I used to make my monitoring widget, starting with some CSS:

<style type="text/css">
me_dashboard_widget p.sub {font: italic 13px Georgia, "Times New Roman", "Bitstream Charter", Times, serif;color: #777;margin: -12px;padding: 5px 10px 15px;}
#me_dashboard_widget .table {margin: 0 -9px 10px;padding: 0 10px;}
#me_dashboard_widget table {width: 100%;}
#me_dashboard_widget td.b {font: normal 14px Georgia, "Times New Roman", "Bitstream Charter", Times, serif;text-align: right;padding-right: 6px;}
#me_dashboard_widget table td {padding: 3px 0;border-top: 1px solid #ECECEC;white-space: nowrap;}
#me_dashboard_widget table tr.first td {border-top: none;}
#me_dashboard_widget td.first,#me_dashboard_widget td.last {width: 1px;}
#me_dashboard_widget .t {color: #777;font-size: 12px;padding-top: 6px;padding-right: 12px;}
#me_dashboard_widget td.b a {font-size: 18px;}#me_dashboard_widget tr.first td {border: none;}
</style>

After which I added a table, which would display the information I wanted:

<div class="table">
<table>
<tr class="first">
<td class="first b">
<a href="http://siteexplorer.search.yahoo.com/search?p=<?php echo urlencode(get_bloginfo('url')) ?>&bwm=i" target="_blank">
<?php echo number_format_i18n($this->get_yahoo_backlinks()) ?></a>
</td> </td>
<td class="t">
<a href="http://siteexplorer.search.yahoo.com/search?p=<?php echo urlencode(get_bloginfo('url')) ?>&bwm=i" target="_blank">Yahoo! Backlinks</a>
</td>
<td class="b">
<a href="http://www.alexa.com/data/details/traffic_details/<?php echo get_bloginfo('url') ?>" target="_blank">
<?php echo number_format_i18n($this->get_alexa_rank()) ?></a>
</td>
<td class="last t">
<a href="http://www.alexa.com/data/details/traffic_details/<?php echo get_bloginfo('url') ?>" target="_blank">Alexa Rank</a>
</td>
</tr>
</table>
</div>

I need to get information from other websites, so I’m going to make an HTTP-Request function that goes and gets the necessary data for me, from those websites (namely Yahoo and Alexa):

function get_http_request($url)
{
$output = false;
if (file_exists(ABSPATH . 'wp-includes/class-snoopy.php')) {
require_once(ABSPATH . 'wp-includes/class-snoopy.php');
$s = new Snoopy();
$s->fetch($url);
if ($s->status == 200) {
$output = $s->results;
}
}
if (!$output && function_exists('wp_remote_fopen')) {
$output = wp_remote_fopen($url);
}
if (!$output && function_exists('fsockopen')) {
$parsed_url = parse_url($url);
$http_request = 'GET ' . $parsed_url['path'] . ($parsed_url['query'] ? '?' . $parsed_url['query'] : '') . " HTTP/1.0\r\n";
$http_request .= "Host: " . $parsed_url['host'] . "\r\n";
$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
$http_request .= "Connection: Close\r\n\r\n";
$response = '';
if (false != ($fs = fsockopen($parsed_url['host'], 80, $errno, $errstr, 10))) {
fwrite($fs, $http_request);
while (!feof($fs))$response .= fgets($fs, 1160);
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
$output = $response[1];
}
}
return $output;
}

After that, all that is needed is to use this function to get the information:

function get_alexa_rank()
{
$current = "";
$response = $this->get_http_request(sprintf('http://data.alexa.com/data?cli=10&dat=snbamz&url=%s', urlencode(get_bloginfo('url'))));
if ($response && preg_match('/" TEXT="((\d|\,)+?)"/', $response, $matches) && $matches[1]) {
return $matches[1];
}
return $current;
}
function get_yahoo_backlinks()
{
$current = "";
$response = $this->get_http_request(sprintf('http://siteexplorer.search.yahoo.com/search?p=%s&bwm=i', urlencode(get_bloginfo('url'))));
if ($response && preg_match('/Inlinks \(([0-9,]+)\)/', $response, $matches) && $matches[1]) {
return str_replace(',', '', $matches[1]);
}
return $current;
}

And the run my class, and close up PHP:

$GLOBALS['me'] = new MonitorEssentials();
?>

And that’s it!

I upload the file to my WordPress Dashboard on one of my blogs, and it works! Hooray.

Thanks for tuning in, let me know if you have any comments on questions; I enjoy hearing your responses in the comments!

Join a community of 3000+ users by signing up to our weekly newsletter!
Or catch us on one of these social networks