How to make a Wordpress plugin with less than 20 lines of code

Posted by Andy Bailey at 7 September, 2007, 12:05 pm
2,046 views

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:
  1. /*
  2. Plugin Name: Hello Stumbler
  3. Version: 1.1
  4. Plugin URI: http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/
  5. Author: Andy Bailey
  6. Author URI: http://www.fiddyp.co.uk
  7. Description: Adds a message to request a thumbs up if post is seen via Stumbleupon. Just place hellostumbler(); in your single post template
  8. */
  9.  
  10. function hellostumbler(){
  11.     global $id;
  12.     $refer=$_SERVER['HTTP_REFERER'];
  13.     $title=the_title('','',false);
  14.     if ($refer=="http://www.stumbleupon.com/refer.php" && is_single()) {
  15.         echo "<strong> Thanks for Stumble-ing here!<br>Please give me a <link rel='plugin' title='Hello Stumbler - Wordpress plugin' href=http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/' /><a href=\"http://www.stumbleupon.com/submit?url=".get_permalink($id)."&title=$title\"><img src=\"http://www.fiddyp.co.uk/icon_su.gif\"/>thumbs up!</a></strong>";
  16.     }
  17. }
  18. ?>

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

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

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:
  1. function hellostumbler() {

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:
  1. global $id;
  2.     $refer=$_SERVER['HTTP_REFERER'];
  3.     $title=the_title('','',false);
  4.     if ($refer=="http://www.stumbleupon.com/refer.php" && is_single()) {
  5.         echo "<strong> Thanks for Stumble-ing here!<br>Please give me a <link rel='plugin' title='Hello Stumbler - Wordpress plugin' href=http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/' /><a href=\"http://www.stumbleupon.com/submit?url=".get_permalink($id)."&title=$title\"><img src=\"http://www.fiddyp.co.uk/icon_su.gif\"/>thumbs up!</a></strong>";
  6.     }
  7. }
  8. ?>

This is a simple little bit of code, lets analyze it line by line...

PHP:
  1. $refer=$_SERVER['HTTP_REFERER'];

all this does is find out what page the surfer came from and store it in the string $refer

PHP:
  1. global $id;

this assigns the variable $id as a global so you can use the value it has been given by the Wordpress code

PHP:
  1. $title=the_title('','',false);

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:
  1. if ($refer=="http://www.stumbleupon.com/refer.php" && is_single()) {

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:
  1. echo "<strong> Thanks for Stumble-ing here!<br>Please give me a <link rel='plugin' title='Hello Stumbler - Wordpress plugin' href=http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/' /><a href=\"http://www.stumbleupon.com/submit?url=".get_permalink($id)."&title=$title\"><img src=\"http://www.fiddyp.co.uk/icon_su.gif\"/>thumbs up!</a></strong>";

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..

  • PHP:
    1. echo "Thanks for Stumble-ing here! Please give me a

    Just output some text for the surfer to see

  • PHP:
    1. <link rel='plugin' title='Hello Stumbler - Wordpress plugin'
    2. href=http://www.fiddyp.co.uk/2007/09/06/hello-stumbler-wordpress-plugin-for-stumbleupon-visitors/' />

    this is put into the source code so search engines and viewers of source can see where the next link comes from. Grabbed this from the Digg This plugin.

  • PHP:
    1. <a href='http://www.stumbleupon.com/submit?url=".get_permalink($id)."&title=$title'>

    At the end of ?url= I close the echo quotes and use a dot (.) operator to add on the next bit that php sees, the get_permalink($id) just gets the link to the current post (it's why we needed to have $id as global, Wordpress has already declared what $id is) then we dot (.) a bit more echo output on to the end of that for the title name $title and close the html quotes with a single quote, and close the html a href bit with a >

  • PHP:
    1. <img src=\"http://www.fiddyp.co.uk/icon_su.gif\"/>thumbs up!</a>";

    echo out html code for displaying the SU icon, close the img HTML and put what text to display as link and then close the html. Finally, close the echo quotes and add a semicolon (;) to tell php we have finsihed the command (echo).

  • PHP:
    1. }
    2. }
    3. ?>

    close the "if" check, close the function and finally end the PHP

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:
  1. if (function_exists(hellostumbler)){hellostumbler();}

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

Popularity: 5% [?]

Category : Code | Wordpress

Related Posts

  • Hello Stumbler - Wordpress plugin for Stumbleupon visitors
  • The love/hate relationship I have with coding..
  • CommentLuv - 1.0 release soon
  • How to stop spam blogs from hotlinking your images with htaccess
  • CommentLuv Wordpress Plugin Updated Please Download
  • Some absolute cracking good plugins and tools for WP site developers
  • comment-luv-plugin

  • Comments

    chase (12 comments.) September 7, 2007

    I am so bad actually when it comes to real coding but this is nice stuff!

    Andy Bailey September 8, 2007

    I’m glad you like it Chase! your template coding is very nice, that’s something I want to be able to do better

    Mathew Browne (1 comments.) September 10, 2007

    Superb work Andy, I wish I could find a spare day to mess around with WP plugins

    Andy Bailey September 10, 2007

    Thanks Mathew, I only discovered how to make plugins recently. Once you the comments in there, it’s just a case of making a function just like a normal php script function.. easy peasy!

    Jalaj (20 comments.) September 28, 2007

    Hi Andy,
    that’s a good step by step tutorial. I have coded in php but never had chance to develop wordpress stuff other than theme (based on Kubric).
    Hope to use this soon..

    Jalaj’s last blog post..Drive Slowly : Google Ahead

    Andy Bailey September 28, 2007

    I was surprised at how easy it was, it really is as simple as making a function and adding the correct comments to the top to make wordpress realize what it is.
    I’ve only learned php from taking apart other peoples scripts so if you’ve played with themes and a bit of php then you should be able to knock one out in no time!

    I might make a picture step by step tutorial for a hello world plugin and continue to expand it over a period of a few weeks, it might make a nice series for here.. hmmmm, I smell some draft posts being stored on my blog coming :-)

    René (1 comments.) October 23, 2007

    this page comes up aparse error

    René’s last blog post..Tut?s mummy on display for 1st Time?

    Andy Bailey October 23, 2007

    thanks rene! fixed it now, I wrote this before I installed a plugin that can execute php code in a post..
    should be ok now

    Hamant Keval (1 comments.) April 25, 2008

    Hi Andy -
    I was surfing and came across your site when someone mentioned your Comment Luv Plugin and came to this post - re- creating a plugin . Although I am not a programmer in any way or form I realised I I would like to learn a little php .

    Thanks for that great tutorial.

    Take care have a great day

    Hamant

    Hamant Kevals last blog post..What’s The Best Way to get Traffic from Ebooks?

    Busby SEO Challenge (5 comments.) August 2, 2008

    This is nice stuff. Explained with great detail. I’m a little unsure what version of Wordpress this was written for? Has anything on plugin development changed since this was written considering we are now on 2.6 ?

    Busby SEO Challenges last blog post..Website Hosting Options

    Blogsdna (1 comments.) August 6, 2008

    Great Article and explained in really simpler meaner, i have one query regarding developing wp-plugin, is it advisable to use CURL php library in wp plugin ? which i am planning to use for one more my 1st wordpres pligun.

    Blogsdnas last blog post..Customize Your Gmail Inbox Web Clips [Gmail New Feature]

    Leave a comment

    7 online now
    the most online was 176
    elottery magnetic Sponsoring
    Sponsors
    available ad space available ad space available ad space available ad space available ad space available ad space