How to create a WordPress Widget

I think it’s time to learn how to create a WordPress Widget. WordPress (WP from now on) is the most used blogging platform, and I want to help everybody to code their favorite widgets.

From the official site (where you can find a lot of widgets): WordPress Widgets (WPW) is like a plugin, but designed to provide a simple way to arrange the various elements of your sidebar content (known as “widgets”) without having to change any code.

From a geeky point of view, a widget is a php file. Fullstop. Being a php file, you can make whatever you want. You “just” need to master php and follow some guidelines given by WP guys.

To make the simplest widget ever, you have to know only two WP functions:

The first is register_sidebar_widget($name, $function): adds your widget in the widget admin panel. You simply have to change $name with the name of your widget and $function with the function that your widget will execute.

The second one is add_action($event, $function): $event tells what event your $function should be associated with.

Ok, now let’s write some php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/*
Plugin Name: Triqui
Plugin URI: http://www.emanueleferonato.com/
Description: Test widget
Author: Emanuele Feronato
Version: 1
Author URI: http://www.emanueleferonato.com/
*/
 
function triqui_widget() {
	echo"<h2>Triqui widget</h2>I love <a href = \"http://www.emanueleferonato.com\">Emanuele Feronato</a>";
}
 
function init_triqui(){
	register_sidebar_widget("Triqui", "triqui_widget");     
}
 
add_action("plugins_loaded", "init_triqui");
 
?>

Lines 2-9: This commented code is needed to show WP users who made the plugin, what does the plugin do, where to download updates, and so on. Look at this picture:

WordPress widget

It represents your plugin management page, and you can see how your information is displayed according to what you write in lines 2-9

Line 11: Beginning of the widget itself

Line 12: The widget… just writing the title (between h2 tags) and a link to my blog

Line 15: Function to initialize the widget

Line 16: As explained before, I am registering the sidebar widget calling it Triqui, and want it to execute the triqui_widget function, the one at lines 11-13

Line 19: When the plugins are loaded, I want init_triqui to be executed. Doing this way, once plugins are loaded, I register the sidebar widget that will display the link at my blog.

And that’s all.

Now you have to save your file with a php extension (for example triqui.php) and upload it in your wp-content/plugins directory you will find in your WP installation.

Then you will find your Triqui plugin in your plugin management page and once you activate it you will find the Triqui widget in your Sidebar Arrangement page (Presentation -> Widgets in your admin panel)

WordPress widget

… and… that’s it… now drag your widget into your sidebar and you will display my link in your blog.

This was the very first widget… but soon I will teach you how to develop more complex and interactive widgets, and, why not, widgets that could you earn some money.

Meanwhile, why don’t you display my widget for a day or two? If you do it…

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (56 votes, average: 4.50 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» WordPress themes are designs for WordPress - one of the most popular blogging software nowadays.
You will be pleasantly surprised by WordPress Themes provided by Template Monster. All of them are of professional design and high quality.

50 Responses

  1. Graham Kaemmer says:

    Just out of curiosity, what do you use to syntax highlight your scripts? Is it something that comes with wordpress?

  2. Emanuele Feronato says:

    It’s called syntax highlighter, google for wordpress syntax highlighter and you will find a lot

  3. Graham Kaemmer says:

    Thanks

  4. Dmitry says:

    Thank for for interesting widget ideas.

  5. Cahyo says:

    Thanks for the tutorial…

  6. EagleVision says:

    LOVE IT! :D

  7. Jogos says:

    I was looking for this type of articles. Now I have got some ideas about creating WordPress widget. Great!

  8. stratos says:

    man nice tutorial. stress out somewhere that the comments are usefull! :D it took me 15 minutes to figure out what was going on… nice one man!!

  9. Mister Chief says:

    Just what I’m looking for. You seem to be able to code in all sorts of languages, your very skilled.

  10. bmattb says:

    thanks for the great tutorial!

  11. bobman says:

    Thank you for this tutorial, but one concern:

    I’ve done exactly what you said, but when I activate the plugin and add it to a sidebar in WP 2.5.1, I then get a blank screen following a save of my new settings.

    Refreshing the page or using the back button let’s me see the admin side of things again, but everytime I change a setting (essentially submitting a form), I get a blank screen…any ideas why?

    Thanks

  12. tektok says:

    Nice, I like it. Keep up the good work.

  13. [...] Biraz araştırma yapınca plugin ve beraberinde widget yapmanın sanıldığından daha kolay olduğunu gördüm.  Öncelikle yararlandığım kaynağı göstermeliyim ki merak edenler kaynağından bakabilirler. http://www.emanueleferonato.com/2008/02/15/how-to-create-a-wordpress-widget/ [...]

  14. [...] on tutorials from Widgetizing Plugins, Lisa Stewart, Emanuele Feronato, and especially Hrw website; here is my first plugin for WordPress: Meta-Lite Widget for your [...]

  15. Joe says:

    Thanks for the tutorial. I wish the guys at WP would learn to write tutorials this simple and concise.

  16. It was really simple to understand. Thanks for the good work. I’m going to start to write a widget now itself. :-)

  17. Steve says:

    Great article thanks. I have used it to code up my own widget that I use to display the latest uploads I do through ShoZu in the sidebar of my wordpress web site.

  18. Travis says:

    Thanks for a great tutorial for beginner plugin developers! Best “first step” resource I could find!

  19. The DarkStar says:

    Penso che ora mi scrivero da solo un widget per il login con tanto di avatar e link utili, grazie dell’articolo ;)

  20. [...] continuing, I suggest you to read How to create a WordPress Widget if you don’t have experience with WP [...]

  21. Yohanes says:

    Thank you for the lovely short tutorial.
    btw, where could i find the next part??

  22. [...] followed this easy tutorial to make it. It was speedily done, and I hope I can sleep [...]

  23. Amy says:

    Cool.. but what about newbies like me? You don’t clarify as to where to paste this code? I can edit what you’ve wrote to fit my needs but how to I create a new php file and such?

  24. Illysmanx says:

    Thanks for the short tutorial! It’d be lovely if you expanded it, with steps on adding small items like a text box or string like in ‘text box widget, one of the defaults.

    Your explanation was a helpful thing. :)

  25. The Cobra says:

    Amy,

    I think you just need a basic text editor. On my mac I personally used Aquamacs Emacs, which is great b/c it has a syntax system that works with several languages.

    With a basic editor, just save it as .php, or if it auto adds .txt, just rename it to .php.

  26. Nik Daum says:

    Thanks so much for the basic code. It really helps me understand what’s going on.

  27. Glenn says:

    You might be interested to know that now you can create a little widget like this in a few minutes. Also the widget you create will be fully themeable.
    Check out the little tool I built to create widgets at widgetifyr.com

  28. sasha854 says:

    Жаль не мое…..

  29. mysoogal says:

    hey can you create a ffmpeg upload widget ? im having hardtimes getting things going

  30. QuickInsider says:

    Hi Emanuele,

    Great tutorial, I will certainly follow-up with the continuation of this tutorial to be able to create Plugins/Widget that will conform to my specific needs.

    Thanks again.

  31. eazeenet says:

    Thanks for such a nice tutorial(simple and sweet).

  32. Daragh says:

    I’ve been trying this code on WP 2.7.1 and am getting the following error:

    Warning: Cannot modify header information – headers already sent by (output started at [myhost]/plugins/daragh.php:31)

    Does this work with WP 2.7.1? It does show up correctly in Admin -> Widgets and appears to work fine up until I add it to the sidebar and click save – which is where I see the above message.

    Oddly enough – it does actually *work* on the page itself, just don’t know why I’m getting the error.

  33. Hi Daragh, I think your problem might be because your php tag doesn’t start on the first line. Make sure you delete blank space before your php tag

    I am not sure how to make a file available for download. I found creating widgets is much easier for Blogger and Typepad. For WordPress, what is the code I would need to make my file downloaded from my server.

  34. Very very very good tutorial… tks!!!

  35. smsfame.com says:

    I want to create a widget to send sms, for blog. but I want to give this feature only to registered user, how to get registration data? how I check user is loged in or not?

  36. [...] how to make a sidebar widget? A quick Google search pointed me here, here, here, here, and of course here. Did I miss [...]

  37. dijifx says:

    Very nice just the basics! I am interested in learning and this tut didnt go overboard.

  38. David Naian says:

    Cool, thanks, it worked just on the first try!!! (WordPress newby)Bye

  39. Great tutorial! it’s a big help for sure.

    Thanks,
    Roger

  40. [...] Como criar um Widget no WordPress Publicado Setembro 21, 2009 WordPress CMS Deixar um Comentário Tags: criar, tutorial, widget, wordpress Tutorial de criação de widget básico. Link [...]

  41. mohit says:

    Great tutorial, I will certainly follow-up with the continuation of this tutorial to be able to create Plugins/Widget that will conform to my specific needs.

  42. Francesco says:

    ciao,
    io nella sidebar ho due colonne..come ne aggiungo una con larghezza pari a quella totale tra le due?

  43. bharadwaj says:

    you are just amazing code junkie… thanks A-LOT for this post

  44. [...] Widgets JUSTIN TADLOCK LONE WOLF DESIGNS VALUMS EMANUEL EFERONATO XAVISYS WP [...]

  45. Sorin says:

    Grazie mille Emanuele. Ho imparato adesso di fare i widgets.

Leave a Reply

flash games company