Have fun and help me to get to 1,000,000 downloads!

How to create a WordPress Widget

Emanuele Feronato Php, Tutorials, WordPress

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

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…

From null to full HTML5 cross platform game

I will take you by hand from the bare bones of JavaScript programming through the creation of a full cross platform HTML5 game, with detailed explainations and source code.

If you don't know where to start, then From null to full HTML5 cross platform game is the book for you.

Comments 63

  1. Post
  2. stratos

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

  3. bobman

    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?


  4. Pingback: Plugin olarak widget yapmak. | WebListele

  5. Pingback: c h u o n g n g u y e n » Blog Archive » Meta-Lite Widget: my first WordPress plugin

  6. Steve

    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.

  7. Pingback: Create a Wordpress MochiAds Leaderboards Widget : Emanuele Feronato

  8. Pingback: Sanningen som jag ser den » Widget

  9. Amy

    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?

  10. Pingback: jmdelisle.com » Mon premier Widget.

  11. Pingback: Cómo crear un widget de wordpress. | Entre Códigos

  12. Illysmanx

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

  13. The Cobra


    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.

  14. Glenn

    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

  15. QuickInsider

    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.

  16. Daragh

    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.

  17. Russell D'Souza

    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.

  18. smsfame.com

    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?

  19. Pingback: Sidebar Widgets | NSLog();

  20. Pingback: How to create wordpress mu-affiliate-platform plugin? | Instant Affiliate Commissions

  21. Pingback: Como criar um Widget no WordPress « Galeria de scripts

  22. Pingback: How to create wordpress mu-affiliate-platform plugin? | Wordpress Help

  23. Pingback: How to make a Sidebar Widget « Knowing more about Wordpress

  24. mohit

    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.

  25. Pingback: 11 Most Common Wordpress Posts | New 2 Wp

  26. Pingback: Useful link from my bookmarks | Sandra Jakovljevic

  27. John

    I intuitively closed tabs to other sites regarding this topic and had an inkling that this would be the most helfpul. It was! You succinct explanation was very helpful. Thanks. Very quality. Nice diagram that showed pointing arrows of where each line if code transduced to the wordpress plugin menu. AWESOME!!!!!!!!!

  28. Cory

    You can simplify this now by only using the wp_register_sidebar_widget() function. No need for the add_action anymore, depending on what your wanting.

    Good post

  29. Pingback: Top 12 Most Commonly Posted Topic Types In WordPress Articles - codeManiac | codeManiac

  30. Pingback: Korisni linkovi za februar | Sandra Jakovljevic

  31. Pingback: 38 Hot Excellent WordPress Tutorials For Both Professionals And Beginners | Creative Verse

  32. Pingback: 38 Hot WP tuts for beginners & pro’s | WordPress Tutorials 101

Leave a Reply

Your email address will not be published. Required fields are marked *