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

September
7

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

Code, Wordpress


0 Comments zu “How to make a Wordpress plugin with less than 20 lines of code”

  1. chase
    07.09.07 3:12 pm

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

    #1

  2. Andy Bailey
    08.09.07 4:56 pm

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

    #2

  3. Mathew Browne
    10.09.07 3:30 pm

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

    #3

  4. Andy Bailey
    10.09.07 3:32 pm

    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!

    #4

  5. Jalaj
    28.09.07 10:17 am

    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

    #5

  6. Andy Bailey
    28.09.07 12:06 pm

    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 :-)

    #6

  7. René
    23.10.07 2:41 am

    this page comes up aparse error

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

    #7

  8. Andy Bailey
    23.10.07 5:50 pm

    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

    #8

  9. Hamant Keval
    25.04.08 1:38 pm

    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?

    #9

  10. Busby SEO Challenge
    02.08.08 10:27 pm

    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

    #10

  11. Blogsdna
    06.08.08 7:28 am

    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]

    #11

  12. Robin Bastien
    12.04.09 2:36 am

    Hey, thanks for the post, I’ve been looking all over for something to check the referring page via PHP, and the traffic from Stumbleupon is usually does wonders

    Robin Bastien´s last blog post..Why content should become your baby

    #12

  13. بیش از 50 مرجع برای یادگیری و استفاده بهتر از وردپرس - تالار گفتمان تخصصی دانشگاه آزاد نیشابور
    15.05.09 10:11 am

    [...] یک پلاگین برای وردپرس نوشتن پلاگین های وردپرس از صفر چگونه پلاگینی فقط با 20 خط کد بنویسیم؟! نوشتن قدم به قدم یک پلاگین برای بوکمارک های اجتماعی [...]

    #13

  14. Link FA | لینک فا » بیش از 50 مرجع برای یادگیری و استفاده بهتر از وردپرس
    18.05.09 1:12 am

    [...] چگونه پلاگینی فقط با ۲۰ خط کد بنویسیم؟! [...]

    #14

  15. The Geared Investor
    21.05.09 2:33 am

    I’m been looking for this plugin for so long! I’m the first to admit that even if I like a site, I sometimes forget to give it a thumbs up for deserving websites. The other one that I like that is catching on is the retweet button at the bottom of posts.

    Amazing detail about the code. You have inspired me to write a plugin tonight while I sip a beer! Cheers!

    #15