I had a truckload of hits come to this site last night for my Hello Stumbler plugin, so I thought I would share with you how to make a plugin so you can publish it, stumble it, digg it and bask in your coding glory as it generates hits to your site!

hello stumbler code


I’ll use my Hello Stumbler plugin as a guide…(now re-done so you can see the code all proper like)

here’s the whole code
[php]

/*
Plugin Name: Hello Stumbler
Version: 1.1
Plugin URI: http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/
Author: Andy Bailey
Author URI: http://www.fiddyp.co.uk
Description: Adds a message to request a thumbs up if post is seen via Stumbleupon. Just place hellostumbler(); in your single post template
*/

function hellostumbler(){
global $id;
$refer=$_SERVER['HTTP_REFERER'];
$title=the_title(”,”,false);
if ($refer==”http://www.stumbleupon.com/refer.php” && is_single()) {
echo “ Thanks for Stumble-ing here!
Please give me a thumbs up!
“;
}
}
?>[/php]

Step 1:
Add the comments to the top of your php file so WordPress knows it’s a plugin..

[php num=1]
/*
Plugin Name: Hello Stumbler
Version: 1.0
Plugin URI: http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/
Author: Andy Bailey
Author URI: http://www.fiddyp.co.uk
Description: Adds a message to request a thumbs up if post is seen via Stumbleupon. Just place hellostumbler(); in your single post template
*/
[/php]

The plugin URL is important, don’t be tempted to just put a link to the root of your site or blog, it’s really annoying when a plugin misbehaves and you click on the plugin name within wordpress only to be sent to a site where you can’t find the reference to the actual plugin.

With mine, I make the post about the plugin and then go back to the source code and add the permalink, zip, upload and finally edit the post with the link to the download.

Step 2: Call the function a unique name
[php num=11]
function hellostumbler() {
[/php]

Make sure your plugin has a unique name or it will misbehave if it’s used on a blog that has a duplicate named plugin.

Step 3: Add the code that does the magic..
[php num=12]global $id;
$refer=$_SERVER['HTTP_REFERER'];
$title=the_title(”,”,false);
if ($refer==”http://www.stumbleupon.com/refer.php” && is_single()) {
echo “ Thanks for Stumble-ing here!
Please give me a thumbs up!
“;
}
}
?>[/php]

This is a simple little bit of code, lets analyze it line by line…
[php num=13]$refer=$_SERVER['HTTP_REFERER'];[/php]
all this does is find out what page the surfer came from and store it in the string $refer
[php num=12]global $id;[/php]
this assigns the variable $id as a global so you can use the value it has been given by the WordPress code
[php num=14]$title=the_title(”,”,false);[/php]
this assigns the title of the post to the variable $title by using the internal function of WordPress, whatever is between the ” , ” is what gets put before and after the title of the post, the false bit means return the title instead of printing it out on the blog.
[php num=15]if ($refer==”http://www.stumbleupon.com/refer.php” && is_single()) {[/php]
a conditional check, if the value in $refer is the same as the one given (in this case the referring page of Stumbleupon) AND the post is a single post page (It’s better to Stumble a post on it’s own rather than when it’s on the front page). If BOTH cases are true, do whatever is in the ‘{‘ curly brackets
[php num=16]echo “ Thanks for Stumble-ing here!
Please give me a thumbs up!
“;[/php]
echo means output to the page, putting html in the quotes writes html direct to the page. Then it’s a simple case of formatting the HTML so it works with the variables declared earlier. I’ll break this line down piece by piece..

Once you’ve done all that, upload the plugin to your wp-content/plugins/ directory and activate it, if all goes well, you should be able to add the output to anywhere on your page by calling the function name with
[php] if (function_exists(hellostumbler)){hellostumbler();}[/php]
inserted into your template code.

This is what it looks like in action…
Hello Stumbler result

So, now there’s no reason why you can’t make a simple plugin to show a link to your facebook profile or add some code to show a Youtube video.

good luck, you’ll see that once you’ve made one plugin, you’ll want to make more and more! :-)

You can download all the source code here : hello stumbler plugin