WP Conditional “Digg This” Plugin: Documentation

The Conditional ‘Digg This’ WordPress Plugin is designed to automate the addition of the “Digg This” badge to popular articles on your WordPress blog.

It makes use of the Digg API and some AJAX requests to find out how many Diggs an article has. Then, some javascript edits the page to either add the “Digg This” badge or delete the empty container.

Outline of the Plug-in’s Operation

Here’s a brief outline of the steps the plug-in goes through during execution.

  • Insert links to external .js files (prototype.js and wp-conditional-digg.js)
  • Add a div container to each post. This container includes a snippet of javascript that calls the checkDigg() function.
  • As the page loads, each article’s javascript snippet calls the checkDigg() function and creates a new AJAX request (using the prototype.js framework).
  • The AJAX request sends the article’s URL to check-digg-stats.php, which checks the Digg API for the number of Digg’s the article has
  • Based on the response, the AJAX request either adds the Digg This badge or deletes the entire container.
Where It All Starts – wp-digg-conditional.php

This is the basic plug-in file. It sets the entire script into motion.

It hooks into WordPress at two points: wp_head and the_content.

At wp_head, we insert two external .js files in the head element. The first one, prototype.js, is an AJAX framework that simplifies the AJAX request process. If you are using prototype.js for something else and have it attached to your site in your template, you can comment out that line.

The second .js file is wp-digg-conditional.js. This actually creates the AJAX requests and does the processing.

The second hook, the_content, modifies each post as it’s sent to the user. It adds a special div container (with id wpDiggBadge###) that we can edit later.

This div container also has a snippet of javascript. This contains a call to the function checkDigg() and sends the link to the article, the post’s wordpress ID, and the root url of your site.

Next Step: wp-conditional.js

This javascript file does the bulk of the work on the page. When checkDigg() is called, this script creates the appropriate AJAX request and does some processing.

It sends an AJAX request to check-digg-stats.php, using the permalink of the article as the parameter. You could optionally add a minDiggs parameter – which determines how many diggs an article needs to have a Digg This badge displayed.

Processing the Ajax Request: check-digg-stats.php

This file processes the request sent by our AJAX object. It sets up a proper request for the Digg API, searches out an individual story with the given URL, and gets its information.

If no permalink is sent to the script, if no story is found in Digg, or if the story doesn’t have a sufficient number of Diggs, an empty string is echo’d as a response to the AJAX request. Otherwise, the script echos the URL to the Digg story.

Handling the AJAX Response

Back in wp-conditional-digg.js, we can handle the response. If the string is empty, we just delete the wpDiggBadge div container. We don’t need it or the javascript anymore.

If we get a string back, we assume it’s the URL to the Digg story. With that in hand, we set about re-writing the page.

First, we delete the javascript already inside the wpDiggBadge div. Then, we create a new iFrame elements with the proper parameters. This essentially does the work that Digg’s .js would normally do to add a Digg This badge to your page.

When the iFrame is created, we insert it as a child element to the wpDiggBadge div. Then we’re done.

Change Log

As new versions are released, you’ll be able to find a change log in one of two places. All changes are listed in the readme.txt file that comes with the plugin’s source code.

In addition, you can check the change log page on this site. Both sources should be up to date.

Bugs, Feature Requests, Questions, Concerns

If you have any bugs, requests, or concerns – leave a comment. I’ll weed through them as I can. I do plan on updating this WordPress plugin a couple of times before it’s “Done” – but it should be in working condition at the moment.

Bookmark and Share:
  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

One Comment to “WP Conditional “Digg This” Plugin: Documentation”

  1. Several Cool Wordpress Plugins Added to LinuxLove Blog | Linux Love said this on

    [...] – plugin’s wordpress.org page – plugin’s homepage [...]

Leave a Reply