built on AIR - - - - so it runs on Win, Mac and Linux:

blago DiggBadge plugin for WordPress

showing a countAfter my adventures in making a TweetBadge, I had to construct my own DiggBadge as well, of course. This one actually Diggs, unlike *some* plugins I’ve seen. And where the TweetBadge only counts retweets coming from clicks on itself, the DiggBadge queries Digg to get the actual dugg-count and the story-page from there.

  • it’s styleable
  • can be placed in a template, i.e. in the footer
  • it can be excluded from specific pages (see options page)
  • the count is stored per page (again, unlike some)
  • works for pages and posts
  • connects to Digg for the actual count
  • submits correct page title and abbreviated summary to Digg

To add the badge to your pages, add this code to a template file:

<!-- diggbadge start -->
<?php if ( function_exists('add_bDiggBadge')) { add_bDiggBadge(); } ?> 

This will include the DiggBadge on both pages and posts, but not on archive or category pages. You can exclude specific pages by their ID on the options page (under Settings in wp-admin). Please see the readme file for all the details on installing.

download blago DiggBadge as a zip (6.72 kB) downloaded 7 times

Pages and posts

I use a code approach for this plugin that you might recognize from Actionscript3: the main plugin php defines a class. The plugin defines a class instance for each page or post that it’s called on. Within the class instance, all variables are safely stored and accessible. That way, I am sure to get unique values for that page – I tested some plugins that returned a clickcount for the whole site, as a global variable… In writing a class, you’d get an instance whose methods you can call directly from your options page for example. It seems to me there’s much less chance of conflicting variables in this way.

A bare-bones class structure for php might look like this:

<?php
/* Badge class */
if (!class_exists("bDiggBadge")){
	class bDiggBadge{
		//list public vars

		/* constructor */
		function __construct(){
			//add actions, filters, calls
		}
		function do_stuff(){
			//plugin logic
		}
	}
}

/* create instance */
if (class_exists("bDiggBadge")){
	$bDiggBadge = new bDiggBadge();
}

/* called from template */
function add_bDiggBadge(){
	global $bDiggBadge;
	if (isset($bDiggBadge)){
		$bDiggBadge->construct_badge();
	}
}
?>

I think writing classes for an application like WordPress is the way to go! It seems much safer than having all those global vars from your assorted-quality plugins on the loose. Well, IMHO.

And yet this plugin is far from perfect… It could use some garbage collection – again, thinking like a flash coder. It is available in WordPress: there’s the delete_option() function to clear stored options from the database, and
I only haven’t had the time to implement them yet.

Getting the dugg-count

Something that puzzled me quite a bit was how to get the response parsed from Digg. Thanks to Terri Ann and her post at blog.ninedays.org I was able to display real counts. Here is my code:

//--- get dugg-count and digg-href from Digg
//tnx to http://blog.ninedays.org/2008/07/30/how-many-diggs-intro-to-using-the-digg-api/

function queryDigg($permalink=NULL){
	ini_set('user_agent', 'blagoDiggBadge-1.0');
	$permalink = empty($permalink)?
'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : $permalink;
	$api_query = 'http://services.digg.com/stories/';
	$api_query .= '?link='.urlencode($permalink);
	$api_query .= '&appkey='.urlencode("http://www.blagoworks.nl/telexer");
	$api_query .= '&type=xml&count=1';
	$result = (array) simplexml_load_file($api_query);
	$info = array();
	if(!empty($result['story'])){
		$result['story'] = (array) $result['story'];
		$info['diggs'] = (!empty($result['story']['@attributes']['diggs']))?
intval($result['story']['@attributes']['diggs']) : 0;
		$info['href'] = (!empty($result['story']['@attributes']['href']))?
$result['story']['@attributes']['href'] : "";
		return $info;
	} else {
		return 0;
	}
}
  • Digg
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Mixx
  • Tumblr
  • Technorati
  • NewsVine
share and connect
Posted in category WordPressTagged Bookmark the permalink
Post a comment or leave a trackback.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please leave these two fields as-is: