246 Responses

  1. The Frosty @WPCult
    The Frosty @WPCult Published |

    I’m going to have to read that again when I am not so tired..

    Good write up.

  2. How to create Widgets in WordPress 2.8 | WordPress Philippines
  3. Stefano
    Stefano Published |

    As usual, Justin: great great article!


  4. BuddyPress Links #12 · Buddypress Links
  5. Dicas, Dicas e mais Dicas | pBlog
    Dicas, Dicas e mais Dicas | pBlog at |
  6. J Mehmett
    J Mehmett Published |

    Enjoy exploring new things and the reason I do love WordPress is that every major release is making even better application.

    Back to the topic, I agree that writing a widget in the WP releases prior 2.8 was really very nasty and unorganized job full of messy functions and dozens of variables.

    I understand things by getting them in my hands first, I have downloaded your code, and it seems as its the sole teacher to understand the tip.

    Thanks for all the good stuff, bro.

  7. The complete guide to creating widgets in WordPress 2.8 | Bloggers Tools
  8. Good Guide for creating Widgets
    Good Guide for creating Widgets at |
  9. Randa Clay
    Randa Clay Published |

    As always Justin, you rock. I’m mildly techie, but I think with your excellent instructions I could do this!

  10. Matt
    Matt Published |

    Thanks for the wonderful break down, this helped me recreate two of my widgets so far (twitter and feedburner). It’s so much easier than WordPress 2.7.

    One problem I encountered is adding onfocus, and onblur to an input field, what would be the properly formatted code? Any help would be appreciated.

  11. WordPress 2.8 - Better, Bolder, and Still in Beta
  12. Matt
    Matt Published |

    @Justin: I thought so too, but when I added this value="Enter your e-mail address" onfocus="if (this.value == 'Enter your e-mail address') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Enter your e-mail address';}". When using the code as is, it gives the following error: syntax error, unexpected T_STRING

  13. Matt
    Matt Published |

    Thanks for the link Justin, I got it to work.

  14. links for 2009-06-03 « sySolution
    links for 2009-06-03 « sySolution at |
  15. chantelle tamaris
    chantelle tamaris Published |

    WoW this is great. I will have to read it again and try along with it ,but I always wanted to make a widget. Something new to learn about Wordpress. Thanks Mr Tadlock!

  16. WordPress 2.8 for theme authors | The Theme Museum
  17. leon
    leon Published |

    Thanks for your guide. I have followed your guide and managed to create multiple instances of the plug-in and had then displayed them on different sidebars.
    But when I try to change the options on one widget on a sidebar … the changes will automatically apply to all the instances of widgets on all other sidebars!?
    Do you have an idea on how to allow the plug-in handle multiple instances of widgets? Or …
    Anyway, I have learnt a lot for the first time about Widget once read this guide. Thanks again

  18. The complete guide to creating widgets in WordPress 2.8 | Totally WordPress
  19. WordPress 2.8: Features and tips — gunnerpress.com
  20. Wordpress 2.8 And 10 Things That You Should Know Before/After You Upgrade
  21. Jennifer
    Jennifer Published |

    This post was really helpful. Thank you! I do have a question though 🙂 How do you add wysiwyg controls to a widget? I’ve tried taking apart a few old plugins – but they don’t use the new 2.8 widget class, so things get kind of messed up. They also seem to link to their own version of tinymce which doesn’t make sense to me, since there is already tinymce bundled with wordpress… why not make use of that? Hoping you can help… 🙂

  22. 10 Incredibly Useful WordPress 2.8 Tutorials
  23. Jennifer
    Jennifer Published |

    I have been able to get tinymce to show up in a textarea of a widget – but when I go to save the widget – it doesn’t seem to save the changes – and then reverts the box back to the non-tinymce view. I’m beginning to think now that this may be some kind of issue with the widget class/feature itself – because that just sounds REALLY odd… I’m not a fan of the wysiwyg editor myself – but my clients can’t do HTML – so I need to have it there for them.

  24. Mikko
    Mikko Published |

    Excellent and easy tutorial. Thanks a lot!

  25. Mikko
    Mikko Published |

    Ok, despite the excellent tutorial, I don’t get it. I’ve created a widget, but when I try to save it, it doesn’t. It looks like it works, but it doesn’t appear on the page and if I reload the widget page, it has disappeared from the sidebar.

    The widget is exactly like your example widget, which of course works perfectly. Any ideas what could cause behaviour like this?

  26. George Serradinho
    George Serradinho Published |

    I also love the new widgets API and the new features on WP 2.8.

    I have bookmarked this post as I will have to read this a couple of times and try and test to see if it works. I would like to create my own widgets as some plugin developers don’t have widgets for the work.

    Thanks for sharing this 🙂

  27. 10 İnanılmaz Yararlı Wordpress 2.8 Dersi « - SamsuNkL.Com -
  28. Max
    Max Published |

    Justin, thanks for the explanation “for dummies” – I really needed it. It seems that WP lacks of better function description and documentation overall… I deal a lot with Drupal, have to say that their API documentation service seems much superior to me than WP.

    Anyway, I’m kinda stuck with updating my own plugin to be compatible with the 2.8 – new widget instances showing up in widgetized areas just fine, but the existing widgets (created in previous WP version) don’t show up and I have no clue why. I output the var_dump for the get_option(“my_widget”) and can see all the existing and the new widgest arrays, but only the new ones show up in the widgets section and the front end. Seems like I can’t even grab their $instance(s). Any thoughts on this?

    Thanks again for the post!

  29. Vasudevbbhat
    Vasudevbbhat Published |

    Hello ,
    its a very good stuff for widgets but how to add this widget or how to install this kind of widget in wordpress 2.8

  30. Nimesh Kumar
    Nimesh Kumar Published |


    Great Stuff !!!!!!!!!!!!

    There is nothing getting stuck at any point……….
    I have created lot of widgets using this one structure…….

  31. Jon Pruett
    Jon Pruett Published |

    Thanks for a great guide! I started my first wordpress widget this week with another template that unfortunately wasn’t ready for the 2.8 widget api changes. But in about an hour I transferred over the work I had done earlier and I now have a multi-instance widget saving and retrieving all the settings I need for the widget I’m creating.

    Thank you!

  32. scriptygoddess » Wordpress 2.8 Issues
  33. Mark
    Mark Published |

    Thanks Justin. This was a nice little tute to get me fast-tracked onto making some custom WP 2.8 widgets.


  34. smsfame.com
    smsfame.com Published |

    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 user registration data? how I check user is loged in or not?

  35. Whirled Wind » Blog Archive » My First Wordpress Widget
  36. Publikační systémy – WordPress 2.8 | Phirebase
  37. Edgar Doiron
    Edgar Doiron Published |

    Can widget be used as part of the loop?

    I’m new to wordpress, so here’s what i want to do.

    Let’s say I have two loops on my page.

    one for displaying important post (6 post)

    another for just daily news (12 post)

    If i’m in the important part and I want to see 6 more post, I click on the page 2 navigation from that section. But I only want that section to reload.

    Are widgets capable of this?

  38. 10 İnanılmaz Yararlı Wordpress 2.8 Dersi « Guncelle
  39. Create a posts by category widget in WordPress 2.8 | Your Site is Valid Blog
  40. P.Raman
    P.Raman Published |


    Please tell me how to create a drop-down author list widget, similar to categories widget.

    Thank you.

  41. links for 2009-07-03 — Chroniques du web
  42. WordPressMU 2.8.1 now out, features and a couple bugs - WPMU Tutorials
  43. Stéphane Gallay
    Stéphane Gallay Published |

    It looks like a neat tutorial, but I’m missing something I haven’t been able to find in any tutorial, namely where do you save your widget code?

  44. Ditutu
    Ditutu Published |

    I’ve created my first widget after reading this post,a widget which shows if the owner of the blog it’s able for freelancer . It’s a very good tutorial ,you are great .

  45. Edgar Doiron
    Edgar Doiron Published |

    @Justin –
    let’s say i want two navigation, one for the top part and one for the bottom part. And having them work independantly?

  46. New And Comprehensive Wordpress 2.8 Tutorial and Hack Toolbox | Graphic and Web Design Blog - Inspiration, Resources and Tools
  47. Johnny Goodperson
    Johnny Goodperson Published |

    Thanks for your tutor mate 🙂
    i’ll make it 😀

  48. Today’s Delicious links - 27/05 | Links | WereWP
  49. New And Comprehensive Wordpress 2.8 Tutorial and Hack Toolbox - Programming Blog
  50. Joe Random
    Joe Random Published |

    Good post for blogger beginners and particularly i like your template which seems to be very elegant for me…

    Thanks for sharing and many more years to come/ your friend krishna das

  51. Joe Random
    Joe Random Published |

    Sorry, one more time- why you’ve not added any ads?

  52. Kevin Baker
    Kevin Baker Published |

    As a blogger who spends hours playing with WP I found this intriquing.

    I am not a comuter prog techie sort but i could follow these instructions and make a good attempt. Maybe i should do that.

    Thanks for the share.


  53. JNFerree
    JNFerree Published |

    I knew there was a good reason why this article/post was in the Top 10 on Stuff-to-Tweet. Nice job Justin. Since my cracker skills are meager at best, I figure I’ll have to send my Rent a Coder buddy here so he can brush up on your How To Make a Widget Work in WP

    How many of your reader’s have asked you what your hourly rate is? BTW, what is it?

  54. links for 2009-07-24 » 4exp.net
    links for 2009-07-24 » 4exp.net at |
  55. Joe Somebody
    Joe Somebody Published |

    Excellent guide to start with wordpress . At firt i feel so tired . Nowwith this tutorial everything is clear .

  56. David Rios (dfrios) 's status on Monday, 27-Jul-09 20:03:34 UTC - Identi.ca
  57. cas
    cas Published |

    This is fabulous! Thank you so much!

    One point of confusion though – there is a discrepancy between the code you show in the post, and the code inside the downloadable example –

    /* Display name from widget settings. */
    		if ( $name )
    			echo 'Hello.  My name is' . $name . '.';
    /* Display name from widget settings if one was input. */
    		if ( $name )
    			printf( __('Hello.  My name is %1$s', 'example'), $name );

    What is the difference between using echo and printf (besides printf using more code)?

  58. Laura
    Laura Published |

    Justin, many thanks for the tutorial – I looked a lot to find how to do this.

  59. Jane Doe
    Jane Doe Published |

    Great and complete tutorial Justin,like always thank you!

  60. GTRway
    GTRway Published |

    Do you know any special plugins to create these widget .That might save a few minutes since i am not much familiar with php and other code?

  61. cas
    cas Published |

    Ah ha! Well, now that makes sense. I see that I have a lot to learn yet about plugins and widgets and Wordpress in general – Thanks so much for taking the time to explain that! 🙂

  62. Jeff @ Code Research Center
    Jeff @ Code Research Center Published |

    Thanks that was just what I needed
    You created a nice and clean example that was very easy to follow.
    I did have one issue but it wasn’t with your code it was with the text editor(visual studio) I was using, switched to phpDesigner and now everything works fine.

  63. Adam
    Adam Published |

    Very good tutorial for me as a newbee.
    I wonder is there any program or plugin to make widget in wordpress?.

  64. Adz
    Adz Published |

    I just want to say thank you! Once I followed it through bit by bit, I began understanding it a lot better, and I’ve nearly completed my first widget.

    Oh, and a little tip for anyone else trying to learn this stuff – don’t just copy paste it. Type it all out bit by bit – it’s invaluable to understand what’s actually going on. That’s applicable to anything really, not just WordPress.


  65. Gaurav
    Gaurav Published |

    Hello Justin,

    I go through your most of the articles and frankly speaking, those are really nice..
    I hope you can solve my one problem.

    I have a plug-in named MM Snippet.
    In Appearance section on widget link, we have list of available widget to be displayed in side bars. In the bottom of the page, the plug-in provides no of Snippets.
    see this link :

    Now the problem is that in available widget page only one snippet is shown. Rest of the snippets are ignored by a condition in function wp_list_widgets()
    This function resides in wp-admin\includes\widgets.php file.

    If I comment the below lines, it works great but I am limited to plug-in only.
    if ( in_array( $widget[‘callback’], $done, true ) ) // We already showed this multi-widget

    I can’t touch the wordpress core code.

    Kindly help..


  66. Gaurav
    Gaurav Published |

    Deleted by the administrator. Please don’t post the same comment twice. This is not much different than having to deal with spam comments on a daily basis.

  67. Gunjan
    Gunjan Published |

    Gr8 stuff….will surly try it out…..sweet and simple

  68. Sarah
    Sarah Published |

    Hello Justin, first let me say how usefull I have found this tutorial.

    Now I managed to follow it and create my own widget succesfully but one little thing. I have a select drop down with options and when I select an option other than the default and hit save although it does save and displays my selection correctly the option in the select drop down (in the widget box-admin) reverts to the default. I think that is confusing for users.

    Then I though let me try your example and see if that happens with your example widget and how strange. It does! I choose female from the drop down and it does save and display correctly however the option in the drop down still shows male.

    Is there a way to fix this little detail? I appreciate any responce to this.

  69. Links for August 11th
    Links for August 11th at |
  70. Create a Social Links Widget in WordPress 2.8
  71. Trabajo
    Trabajo Published |

    Very good tutorial for me, I wonder is there any program or plugin to make widgets for wordpress blogging platform.

  72. Webdesigner
    Webdesigner Published |

    Wow, thanx for this tutorial! I can’t wait to get my hands on this at evening!

  73. willsy
    willsy Published |

    Very nice tutorial. I am trying to extend this and provide file upload by adding a new form item with type=”file”. My form displays and I can choose a file to upload, however I don’t know how to pull out the file information in the update() method. I also cannot find a single example of a 2.8 widget with file upload capability. Any pointers please ?

  74. Raymond
    Raymond Published |

    Here is one for you. Used this code to develop a plugin for use with my Wordpress MU install. The problem is though that when I change the values of parameters on one blog it changes it on all of them.

  75. Edward Caissie
    Edward Caissie Published |

    Just wanted to say “Thanks” for this post, I found it very helpful with adding / creating widget functionality in the plugins I recently wrote.
    Since reading this post a week and a half ago, I have been able to put three plugins into the WordPress repository.
    Kudos to you!

  76. World of Wordpress at Helpful Technology
    World of Wordpress at Helpful Technology at |
  77. 10 Most Helpful WordPress Design Blogs | Cosmos Blog -- Internet News,Life,Culture,Polices,Resource,Make Money
  78. Len
    Len Published |


    I could not find any information on how to install new widget (not present in WP 2.8.4)
    I downloaded a new widget, but don’t now where to put it. Does it go to a theme directory?


  79. Dustin Dempsey
    Dustin Dempsey Published |

    @ Len,
    You add the file to your plugins directory or add the coding to the bottom of your functions.php file in your theme’s folder (This would make it only available on that theme).

    Thanks for the writeup. It helped me out a lot.

  80. Darshana Gunawardana
    Darshana Gunawardana Published |

    This is great. Specially very good tutorial for me.

  81. Ten Tips for Becoming a Better WordPress Developer | Web Design Ledger
  82. Tutorial: Widget selbst erstellen (Wordp… | Wordpress Lesezeichen
  83. CHRISTMAS 2009 − Links for 2009-11-10 [del.icio.us]
  84. elliot condon
    elliot condon Published |

    great tutorial. The new way of making widgets makes a lot of sense coming from a programming background

  85. amber
    amber Published |

    your download link appears to be broken.

  86. neha
    neha Published |


    Your download file contains only one file(example-widget.php). Is that sufficient to run this widget? I am getting the following error

    “Call to undefined function add_action()”

    could you please give me latst download version?


  87. Tom
    Tom Published |

    WOW Thank you sooo much I finally was able to get this working with my plugin!!!! You ROCK!!!
    My plugin:
    mp3 player plugin for wordpress
    next version with these updates is 1.2.5 be on the lookout!

    once again thank you so much!

  88. beep
    beep Published |

    clear explanation…Had some issues but all ok now


  89. andre
    andre Published |

    Really nice tut! Been fighting with finding widgets I could use for custom needs.

    I was able to use your widget and by replacing all “example” with a name of my own it’s working. It’s showing on the sidebar and doing exactly what it should. Thanks!


    Now for the fun part:

    I’d like to have the widget be activated per user’s page. Is there a way it could be draw the info automatically from their registration?

    Not sure if I say it right.

    What I’m trying to create is a profile-maker for artists that would then show up on a single page using the format http://www.mydomain.com/artistname.

    Any ideas?

    thanks again!


  90. Sébastien Méric
    Sébastien Méric Published |


    It seems that there was a modification of the __checked_selected_helper function (wp-admin/includes/template.php) in wp 2.9. The if ( $helper == $current) test as been replaced with if ( (string) $helper === (string) $current) test so the checked( $instance['show_sex'], true ) doesn’t work anymore ! I think that you will have to replace it with checked( $instance['show_sex'], 'on' ) for checkboxes to be checked again…

    Damn it ! If I’m right, we will have to rewrite many widgets code….

    Thank you for all you do to teach wp to the community.


    1. adrian
      adrian Published |

      yes. i believe that is true. urgh

      1. adrian
        adrian Published |

        yup. be careful of true vs ‘on’

        in fact, it seems that on (without quotes) is also adequate.

        seems like a simple #def true on

    2. Rakesh Raja
      Rakesh Raja Published |

      Bro you saved my life man.. yes this is true…

  91. CoreBloggers
    CoreBloggers at |
  92. Rick Falkvinge
    Rick Falkvinge Published |

    Thanks a million, man! This saved my day and I was able to widgetize and parameterize a hardcoded hack from a previous blog in under an hour, despite having no PHP experience.

  93. Fairweb
    Fairweb Published |

    Great guide Justin ! Thanks !

  94. Brian
    Brian Published |

    The widget works great, but I’m having a minor display issue in the WP 2.9 widgets panel.

    See screenshot here: http://twitpic.com/yw8g9/full

    Notice firebug displays inline styles: width: 330px; margin-left: -65px;

    These inline styles are only being added to the example widget downloaded here, and not the other wordpress default widgets.

    What could be causing this? How can I remove this inline code?

  95. Tusday’s Titanic Links! « Sugarpill Factory Blog
  96. jommy » The complete guide to creating widgets in WordPress 2.8
  97. Event Timer Widget for Wordpress | Mark Hedley
  98. Rupak
    Rupak Published |

    Thank you so much for this wonder full article. It really helped me a lot and made it so easy to create a re-usable widget in WordPress. Keep up the good work and you rock!!

  99. Paul
    Paul Published |

    Thanks for the tutorial, but I’d hardly call it the complete guide as you do not mention how to make it visible in the add widget screen.

  100. virg
    virg Published |

    It works! Thanks.

    Is there a way to just add some default php code in the widget? I’m trying to create widget that will show subpages.

  101. Mike Smith
    Mike Smith Published |

    I’m putting this to good use for my theme site. Thanks for posting this up. I’ve customized it a bit. Once it’s released, I’ll shoot you a link. I appreciate you putting this article up, without it I would have been lost 🙂

  102. Mike Smith
    Mike Smith Published |

    Shit, sorry about commenting again so soon Justin.

    I’ve got the widget working, but had a question. For those who want to write php codes directly into their theme without using a widget, how would I go about this? I know the code should be like this normally:

    if(function_exists(‘function-name’)) { function-name(); }

    But I can’t seem to figure out which function name I’m suppose to use and also, inside the function-name() I’d love to be able to allow the users to add their own name/sex right inside (ie: function-name(‘mike’,’male’);

    Is this a simple fix, or something that takes a fair bit of editing?

    Thanks for any help you can give Justin.

    1. frq
      frq Published |

      Mike, I don’t know exactly what you want to do, but you might want to take a look at this plugin: http://wordpress.org/extend/plugins/exec-php/


  103. frq
    frq Published |

    Hello, Justin, I know this article has been out for a while, but it really helped me. Thanks very much.

    I noticed something though. My checkbox was not updating and was off all the time. I changed a little the code (copied from the default Text Widget) to something like this:
    line 97, from:

    $instance['show_sex'] = $new_instance['show_sex'];


    $instance['show_sex'] = isset($new_instance['show_sex']);

    line 136, just the checked part of it, from:


    Then it started working. I don’t know if it has something to do with 2.9, but it worked for me that way.

    Thanks again,

    PS: Checking my writing for the last time before hitting the submit button got me wondering what one might think seeing just these lines of code with a variable called $show_sex 😉

  104. frq
    frq Published |

    Well, i’ve put some ?php tags that got stripped out my comment. My bad. Here is the change in the code on line 136, inside the php tags of the checked part:

    checked( $instance['show_sex'], true );


    checked(isset( $instance['show_sex']) ? $instance['show_sex'] : 0  );


  105. Bookmarks for February 16th from 22:43 to 22:43 | Brewerj Web Design
  106. 11 Most Common Wordpress Posts | New 2 Wp
  107. dandy
    dandy Published |

    My Sex selectbox (male, female) isnt updating. I am using WP 2.9.2.

    The checkbox “Show Sex” was also not updating until i read the comment of @frq.

    What can be the reasons ?


  108. dandy
    dandy Published |

    i fixed it by replacing

    $instance[‘sex’] in selectbox drop down.


  109. dandy
    dandy Published |

    Also how can i make this widget singleton?. Like i want only once instance of this widget to be availabe in widget section.

    I’ll be really grateful if somebody can help.


  110. Owen Hoskins
    Owen Hoskins Published |

    Hey thanks for the post. I had no problem setting up a new widget with this method.

    However, what I am ultimately trying to do is create the equivalent of the text_widget for the dashboard… like the Quick Press but for the sidebar.

    I’ve read up on creating dashboard widgets (http://rick.jinlabs.com/2009/02/01/how-add-options-to-your-wordpress-27-dashboard-widgets/) and in the wp API but what I fail to catch is how and if dashboard widgets can stand in for the text widgets you find in the widgets panel.

    This should be possible yes? Any ideas?

    I think this could be very interesting for providing easier-to-use widgets for the user client.


  111. Lee
    Lee Published |

    Hi Justin

    I’ve been playing around with the example widget code and trying to make a recent posts widget which uses data from custom fields. How do you get custom field data into a widget?

  112. Customizing P2 – The Magic Behind “Sleep” – Will Anderson
  113. Joey Novak
    Joey Novak Published |

    Awesome Post! Just what I needed. Extremely Clear and concise.


  114. Johnathan Andersen
    Johnathan Andersen Published |

    Important note about WordPress widget id_base names. DO NOT include capital letters in the id_base name. When the widget is registered, the id_base name appears to be stored in lower case letters as the wp-options key.

    So the next time you refresh the widgets page or your website, you will not see your widget because WordPress is not looking for your id_base with the capital letter in it.

    Just a helpful tip for anyone else who spent 2 hours trying to figure it out.

  115. Monpelaud
    Monpelaud Published |


    I try to create my first widget with the help of your good tutorial.
    With my widget I have an issue, it’s impossible to save widget options with IE8 !
    When I test “Example Widget” I get the same issue, impossible to save the options.
    Can you help me ?

    Best regards

  116. Carla
    Carla Published |


    Being very new to WordPress I have a obvious question.
    Yes I downloaded your code, Yes I activated the widget in the admin panel but HOW exactly do I use it? Where do I see the form that allows to enter a name …. and will display name, sex etc. etc. A more detailed description of HOW to use widgets would be appreciated since this is an introductory tutorial.



  117. Yvonne
    Yvonne Published |

    I have to be honest that I am literally a beginner of PHP and I was able to use this tutorial to pull off a customizable widget for my wordpress blog.

    Thank you for posting this, this is a great tutorial.

  118. Robert Heller
    Robert Heller Published |

    I am unable to get a widget I created to work properly. I followed the example in this article. The widget shows up just fine in the backend’s Appearence=>Widgets screen and I am able to drag it to one of my sidebars. What happens is that the widget’s output is not showing up on the front end (and I checked the HTML source and there is no sign of if it at all, not even the tags generated from the $before_widget and $after_widget variables). I checked apache’s error_log for possible PHP error messages. Nada. What also happens is if I navigate someplace else on the back end and then navigate back to the widget screen, the widget has vanished from the sidebar. I have been staring at this code for hours and hours and cannot see what is wrong.

    Here is the code:
    /* Add our function to the widgets_init hook. */
    add_action( 'widgets_init', 'load_deepwoods_GAN_Widget' );
    /* Function that registers our widget. */
    function load_deepwoods_GAN_Widget() {
    	register_widget( 'Deepwoods_GAN_Widget' );
    class Deepwoods_GAN_Widget extends WP_Widget {
      function Deepwoods_GAN_Widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'deepwoods_gan_widget', 'description' => __('Display GAN links', 'ganads') );
        /* Widget control settings. */
        $control_ops = array( 'width' => 200, 'height' => 150, 'id_base' => 'deepwoods_GAN' );
        /* Create the widget. */
        $this->WP_Widget( 'deepwoods-gan-widget', __('GAN Ads', 'ganads') , $widget_ops, $control_ops );
      function widget ($args, $instance) {
        echo $before_widget;
        //echo $before_title . $after_title;
        global $wpdb;
        $GANAds = $wpdb->get_results("SELECT * FROM " . DEEPWOODS_GAN_TABLE, 'ARRAY_A');
        if (! empty($GANAds) ) {
          echo '';
          $testside = $instance['ulid'] == 'GANright';
          foreach ((array)$GANAds as $GANAd) {
            //if ( $GANAd['StartDate'] > today && $GANAd['EndDate'] < today ) continue;
    	if ( $GANAd['side'] == $testside) {
    	  echo '<a href="' . $GANAd['ClickserverLink'] . '" rel="nofollow">' . $GANAd['LinkName'] . '</a>' . $GANAd['MerchandisingText'] . '';
          ?> 'GANright' );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
    	<label for="get_field_id( 'ulid' ); ?>">Side:
    	<select id="get_field_id( 'ulid' ); ?>" name="get_field_name( 'ulid' ); ?>" class="widefat" style="width:100%;">
    	    <option >right
    	    <option >leader
        prefix . "DWS_GAN");
    function deepwoods_make_GAN_table () {
      $columns = array ( 'id' => 'int NOT NULL AUTO_INCREMENT',
    		     'Advertiser' => 'varchar(255) NOT NULL' ,
    		     'LinkID'     => 'varchar(16)  NOT NULL' ,
    		     'LinkName'   => 'varchar(255) NOT NULL' ,
    		     'MerchandisingText' => 'varchar(255)' ,
    		     'AltText' => 'varchar(255)' ,
    		     'StartDate' => 'date NOT NULL' ,
    		     'EndDate'   => 'date NOT NULL' ,
    		     'ClickserverLink' => 'varchar(255) NOT NULL' ,
    		     'ImageURL' => 'varchar(255)' ,
    		     'ImageHeight' => 'varchar(255)' ,
    		     'ImageWidth' => 'varchar(255)' ,
    		     'LinkURL' => 'varchar(255) NOT NULL' ,
    		     'PromoType' => 'varchar(32) NOT NULL' ,
    		     'MerchantID'  => 'varchar(16) NOT NULL',
    		     'side' => 'boolean NOT NULL',
    		     'PRIMARY' => 'KEY (id)'
       global $wpdb;
       $sql = "CREATE TABLE " . DEEPWOODS_GAN_TABLE . '(';
       foreach($columns as $column => $option) {
    	$sql .= "{$column} {$option}, ";
       $sql = rtrim($sql, ', ') . ")";
       if($wpdb->get_var("SHOW TABLES LIKE '" . DEEPWOODS_GAN_TABLE . "'") != DEEPWOODS_GAN_TABLE) {
    	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  119. Robert Heller
    Robert Heller Published |

    Damn. The code seems to have been mangled. The code is available in this ZIP file: ftp://ftp.deepsoft.com/pub/deepwoods/Other/GAN_Widget.zip.

    1. Robert Heller
      Robert Heller Published |

      I’ve made some (minor mostly) changes to the GAN code. It still does not work. I made it into a plugin and it is available at ftp://ftp.deepsoft.com/pub/deepwoods/Other/GAN_Plugin-0.0.zip.

  120. Simple Twitter Widget – now as Wordpress plugin | chipsandtv.com
  121. WP Widget: Load posts in a category
  122. David Siegfried
    David Siegfried Published |

    Thank God for this post, I needed a good framework to start with. Lots of stuff out there for earlier versions of WP, moving on to 3.0 I hope this all still holds true, thanks man.

  123. Creating widgets in WordPress | DevReference.org
  124. Ken Kennedy
    Ken Kennedy Published |

    OK, I must be a complete idiot. Where do you save the example widget file, or how do you get it to display in the admin widget area?

  125. Ken Kennedy
    Ken Kennedy Published |

    Well, to answer my own question, since I’m using WordPress MU, it apparently has to go under the mu-plugins directory.

  126. Dineshkumar
    Dineshkumar Published |

    I am using select box to list the category names in my widget.For instance ,select box list 3 categories.I have added 2 widget in my sidebar.When i click widget edit in admin page it displays the categories as in order.I want select box to display what i chosen for that widget . Please help me to fix this….

  127. Dineshkumar
    Dineshkumar Published |

    Can one tell “where the widget data is stored in Database”?

    1. Eleven
      Eleven Published |

      It is located in the wp_options table, the ‘option_name’ is widget_example-widget.
      I have a second question, is it possible to change the ‘widget_’ prefix? Where is it in the code?

  128. meilione
    meilione Published |

    thanks very helpful, just created my own little widget.

  129. Gaurav
    Gaurav Published |

    I m also getting this type of error

    Call to undefined function add_action()

    please give me any solution

  130. Elio
    Elio Published |

    There’s a typo on the SELECT widget control. The lines

    <option >male
    <option >female

    Should be rewritten to

    <option >male
    <option >female

    Otherwise, although the correct option will be stored, the SELECT control won’t reflect the change.

  131. Elio
    Elio Published |

    Bummer, code got stripped. Anyway, these sentences:

    'male' == $instance['format']
    'female' == $instance['format']

    should be

    'male' == $instance['sex']
    'female' == $instance['sex']

    for the widget drop down control to work properly reflecting the correct option selected.

  132. Linkdump for July 29th at found_drama
  133. Sebastian
    Sebastian Published |

    Awesome guide! Thanks a lot!

  134. Josh
    Josh Published |

    Wow! Really easy-to-read, informative guide! Able to read it, follow it and implement really quickly, thanks very much!

    Quick …. must bookmark your site 😉 !

  135. Ben Jackson
    Ben Jackson Published |

    The amount of positive feedback above is well deserved. This is a great tutorial. Thanks.

  136. Sana
    Sana Published |

    Excellent tutorial!

  137. Rafael Minuesa
    Rafael Minuesa Published |

    Thank you so much for the guide.

    I had been away from the WordPress scene for a while (been doing Joomla instead) and I wasn’t aware of just how simple creating widgets now has become.


  138. Drakezilla
    Drakezilla Published |

    Hey dude, thanks for tutorial! i’m creating my own widget right now! 😀

  139. Josh
    Josh Published |

    Thanks for a great article! I am having lots of fun playing with this tutorial.

  140. Ed Tiley
    Ed Tiley Published |

    Thanks to your post I’ve been able to create a widget to display a mini catalog. Which is working nicely.

    And once again, I’ve learned a boatload from dissecting your code. Even though you don’t document it, the translation __() form was an invaluable lesson.

    Now, I’d like to learn one more thing, namely how to add user controlable styling to the widget without without the user having to edit style.css so it is portable from theme to theme without having to alter the theme files.

    Since there’s no HTML section, how would one go about adding CSS to the widget’s output intrinsically?

    Thanks again,


  141. ashish
    ashish Published |

    Thanks…. its a realloy nice tutorial for widget …. Keep writing in such a simple maaner 🙂

  142. Chad Huntley
    Chad Huntley Published |

    Excellent guide, this was very helpful in creating a new widget for BuddyPress

  143. Leslie
    Leslie Published |

    Great guide, thank you. This helped me get started with wordpress widgets.

  144. Luke
    Luke Published |

    A note to anyone who is having trouble saving their widget or getting the settings to save:

    I found that the $control_ops settings made my widget not save, specifically the id_base setting. Try shortening or changing your widget id_base.

  145. Francesco
    Francesco Published |

    I’ve written an abstract class that extend WP_Widget and speed up widget devel

    What you think abuot it? Thanks for this guide.

  146. Steve Lambert
    Steve Lambert Published |

    Just wanted to say thank you, I used this to solve a very particular problem for a custom theme.

  147. teebee
    teebee Published |

    @Ed Tiley and others. I too needed styling for my widget and came up with this solution. I’m not sure that it’s the “correct” way to do it but it works. Put your css file in the same folder as your widget and then in your widget code add this at the top:

    add_action( 'widgets_init', 'init_my_widget' );
    And then added this function:
    function init_my_widget() {
    		/* Load our stylesheet. */
    		wp_enqueue_style('My_Widget_Stylesheet', WP_PLUGIN_URL . '/my_plugin_dir/my-plugin.css');

    If anyone has suggestions to doing this better I welcome the comments. I’m writing my first widget so I know I have lot’s to learn!

    PS: Justin, thanks for an excellent tutorial!

  148. teebee
    teebee Published |

    @Ed Tiley, re-reading your post I see I didn’t really answer your question with my previous post. I’m wondering if you can’t just provide the user with a text block to input their css and then just echo it out in a style block in your widget code? This is not really “standards compliant”, though it would probably work.

  149. DavidM
    DavidM Published |

    Okay, I’ve been searching for more articles on creating widgets with 2.8 and greater. The older widget tutorials are still too common. Good work.

  150. Santosh kumar sahoo
    Santosh kumar sahoo Published |


    I have a problem.

    When i’m trying to upload a file from the widget control, by writing HTML codes in the function form(), i’m not getting anything when i print $_FILES …..

    Can anyone tell me how can i successfully use inside this class.

    Any replies will be highly appreciated.


  151. Thomas Willers
    Thomas Willers Published |

    Thank you Justin,

    I solved a problem for a customer with the help oft this block. I took a lot of hours, but now it looks brilliant.

    Thanks a lot! Greetings from Munich!


  152. JT
    JT Published |

    Thanks Justin for the article.
    Now I am novice to wordpress. Please let me know where should I copy the .php to make it appear on my admin page widget links..

    Thanks in advance..

  153. Consolidating multiple WordPress blogs using categories
  154. AJi Prabowo
    AJi Prabowo Published |

    Great resource!! posting it to my blog.


  155. danny
    danny Published |

    um… Example-Widget doesn’t remember my Gender and Display Sex settings. Always defaults to male / no.

    This is without changing a thing in the download above. I’m using WP 3.0.4

    Just lettin’ ya know. I would like to learn from this example though and a fix would be great. Thanks.

  156. Which form elements are admissible in a widget control form? (two part question) | WordPress Stack Exchange Monitor
  157. Richard Vencu
    Richard Vencu Published |

    Nice information, thanks.

    I had experiences with losing widget information. They were usually due to uppercase letters in the IDs of the sidebars. Check that first.

    Then maybe is a good idea to rename the widget ID in lowercase.

  158. Amit
    Amit Published |

    I have created my first widget studying your tutorial 😀 thanks buddy

  159. Build your own WordPress Widget | Panton Interactive Blogs
  160. Ryan
    Ryan Published |

    Great post – still timely for WP 3.1.
    One problem: when I add custom widgets according to the examples, it makes all of my widgets appear without any form fields in the WP widget screen, ONLY when they’re first added to widgetized area. Then, once I click the save button, the form fields appear and all is well.

    Any idea what’s causing this?

    It doesn’t look like there’s much activity here lately, but hopefully someone can provide an answer..

    Thanks for the great post!


  161. kuldeep
    kuldeep Published |

    Is there any way to get widget form field value outside the widget?

  162. Brent Lineberry
    Brent Lineberry Published |

    Excellent. This article, the WordPress codex, and the WordPress files have greatly improved my understanding of how to build WordPress widgets.

    Thank you.

  163. rakesh kumar
    rakesh kumar Published |

    This is one of the simplest tutorial for a new comer like me. I am going to convert some of my functions into widgets. as the functions have to be placed by changing code in the files.

    Thank you very much for such a simple code.

  164. Jelle
    Jelle Published |

    Thanks for the great tutorial! I don’t understand all of it yet. But it is a big help.

    First I couldn’t make it work. I copy-pasted the code into widgets.php and include_once it in my functions.php. It didn’t appear in the admin area and it broke other parts of the dashboard.

    Then I changed the filename to custom-widgets.php, included it in my functions.php. And… it worked!

    Just posting this for people not getting it to work either.

  165. Ray’s Login Widget « The Mercer Family
  166. Get the latest comments for a custom post type in WordPress
  167. Jon
    Jon Published |

    Thanks for this. Over two years old and still a great, straight forward, easy to follow tutorial.

  168. Mehak Gupta
    Mehak Gupta Published |

    It’s really great tutorial….thanku so much……keep on writing…..

  169. Ashton
    Ashton Published |

    Thanks for this! Helped me to wrap my head around building custom widgets. I’ve already built a couple for a client that will allow them to drop in their call-to-action widgets to their site complete with user-controlled linking for the banners. I’m also building a plugin that will house a selection of widgets for an affiliate program they are launching shortly.

  170. Custom Wordpress Widgets for Newbs « True Confessions of an IT Newb
  171. Melanie
    Melanie Published |

    Hey Justin, this is awesome. I’m in the middle of making a website for a music label and have really tried to push myself to make Wordpress into a real CMS for them. Your site has given me just about everything I need to do this, from this post to your info on custom fields and posts. Thanks so much for taking the time to provide clear, elegant examples, walkthroughs and explanations 🙂

    I’m a big fan.

  172. How can I add links to widget titles? | SeekPHP.com
  173. Kedar Sapkota
    Kedar Sapkota Published |

    I need to development widget for wordpress named social-friend, I used this tutorial to learn. Thanks body.

  174. Francis
    Francis Published |

    Hi Justin,

    Thx for the tutorial, really helpful. I tried to change an input line to get a textarea but when I update the content by validating it, it disappears. Could you help me here ?

    Thanks ! 😉

    1. Chris
      Chris Published |

      Someone earlier had asked a question about adding a text area…so in case anyone still needs this:

      <textarea class="widefat" id="get_field_id('text2'); ?>" name="get_field_name('text2'); ?>">

      basically the textarea needs a full closing tag, whereas the input tag uses the minimized tag.

      So not:

      but use

  175. 100+ Resources for WordPress Theme Developers | E-SEOPRO
  176. adrian
    adrian Published |

    hey. nice tutorial. thanks for the time.

    im having trouble calling a filter from a widget. is it possible. does it run too late.

    function widget( $args, $instance ) {

    add_filter( ‘the_content’, ‘filter_the_content’);

    //do whatever
    function filter_the_content($content) {
    $bacon = ‘ bacon is good’
    return $bacon . $content;

    sorry for posting this here. but seems like you are the widget man

  177. Joe
    Joe Published |

    i was searching for the tutorial to get started with wordpress plugin development. My friend suggested me to have a look at yours. I haven’t gone anywhere else and got satisfied with the result. Cheers!! Thanks for nice tut..

  178. brent
    brent Published |

    Hey Justin,

    I just bought the book you wrote with Brad and Ohz, and so far it is quite helpful, but the one thing that is not covered with regards to widget creation, is how to utilize multiple checkboxes that are dynamically created.

    Basically I need to dynamically create a list of checkboxes, that will then be used to toggle the display of items on the frontend of the site.

    I do not know how many items, or what any of their names will be.

    Here’s an example from my own code I am trying to get working:
    (I’ll leave out the bits that don’t apply – also please note that are supposed to represent the open and close p-h-p tags)

    function form( $instance ) {
    	$instance = wp_parse_args( (array) $instance, $defaults );
    	echo ''.__('Display the following:', 'my_plugin');
    	echo '';
    	foreach ( $items as $item_type => $item ) {
    		$label = isset( $item['title'] ) ? $item['title'] : $item_type ;
    		$chk = 'item_display_' . $item_type;
    		<input type="checkbox" 
    			name=" echo $this->get_field_name( $chk );" 
    			value=" echo $instance[$chk];" 
    			 echo checked( $instance[$chk], TRUE );/>
    		 echo __($label, 'my_plugin');?>
    	echo '';
    function update( $new_instance, $old_instance ) {
    	$instance = $old_instance;
    	$instance['title'] = strip_tags( $new_instance['title'] );
    	foreach ( items as $item_type => $item ) {
    		$chk = 'item_display_' . $item_type;
    		$instance[$chk] = strip_tags( $new_instance[$chk] );
    	return $instance;
    function widget( $args, $instance ) {
    	foreach ( items as $item_type => $item ) {
    		$chk = 'item_display_' . $item_type;
    		if ( $instance[$chk] == TRUE ) {
    		// display item

    the last part doesn’t even matter at this point because I am unable to even get the widget to save it’s values.

    I’m only about half way through your book, but am pretty certain I’m not going to find the answer since the index only lists one reference to checked() on page 70.

    So how? how? HOW? do you dynamically create checkboxes in a WP Widget?

    Thank you in advance for your time, effort and talent.

    1. amit patel
      amit patel Published |

      What is array use of this method?

  179. brent
    brent Published |


    my attempt to sneak in tags didn’t work

    1. Sanam Maharjan
      Sanam Maharjan Published |

      Hello Brent,
      Any luck with this multiple checkbox issue. I also have tried many way around but could not get hold of it. Would be very grateful for any leads from anybody.

      Thanks you 🙂

  180. James
    James Published |

    This was perfect – I can’t believe how wasy it is to build a custom widget. Thanks.

  181. Wordpress Widgets « netzdings.de
    Wordpress Widgets « netzdings.de at |
  182. Edson
    Edson Published |

    I have developed a widget but I want to use it more than once. Is there a parameter or something that tells WP how to keep it in widgets panel while I place it on sidebar as many times as I want? Tks!

  183. WordPress ile ilgili takip edilesi 10 muhteşem blog | Blog Wolkanca
  184. Matty
    Matty Published |

    Hey Nice article, however i am completely new for creating new widget, so want to know the exact file where we need to write this all widget related code.. is it in widget.php or there should be new created file?

  185. Writing a simple widget « Programming
  186. Shaun
    Shaun Published |

    Hi Justin,
    As always your tutorials are the best….
    Not sure if your still supporting questions on this old of a tutorial but I was wondering if its possible to port an image upload button into the widget such as the way the tinymce works? If so how can this be achieved?


  187. Random Joe
    Random Joe Published |

    heh i really wondered how can one give such a simplest steps to create widget for wp….u rock man..!!
    i just created my own widget.,……thankx a lot

  188. Steven Gliebe
    Steven Gliebe Published |

    Thank you for this guide. It was really helpful to me a couple months ago.

    I’m wondering what you think about widget classes being pluggable (using class_exists() in the same way that function_exists() is often used) as a very simple way for child theme hackers to modify a theme’s widgets. Is there anything unwise about this?

  189. Steven Gliebe
    Steven Gliebe Published |

    Thanks Justin. I appreciate your insight.

  190. Laura
    Laura Published |

    Many thanks Justin, this has helped me out a lot.

  191. Khaild
    Khaild Published |

    Hi, Can You update this tutorial so it can be use in wordpress 3.4 ?

  192. Matt Smith
    Matt Smith Published |

    Fantastic publish, thank you Justin! Are there any books or other tutorials you would recommend I check out to learn more?

  193. Milap
    Milap Published |

    Thanks a lot for code, this is what i was looking for..
    Regards from Milap

  194. David
    David Published |

    Hi Justin,
    Great article! I just wanted to know if its possible to do the same thing without using Object Oriented Programming? I am sure that there are many of us who are still new to OOP concepts & can find it really hard to follow the coding style of the current code. So if you can release a non-OOP version of the code, that would be fantastic & highly appreciated.

    Thank you.

  195. Aykay
    Aykay Published |

    Hey Justin. I am a total noob and know absolutely nothing but i am very interested…. can you please post a link where i can gain a bit of knowledge and understand your tutorials? Thank you in advance.

  196. Patrizio
    Patrizio Published |

    Really a good article! Ready to make my very first widget! Thank you!!!!

  197. Andrew
    Andrew Published |

    Thanks for the awesome tutorial! Seems it’s not so complicated after all. 🙂

  198. Rakesh Raja
    Rakesh Raja Published |

    Hi Justin

    Thx a ton for such a wonderful tutorial. I am stuck with one problem.. That check box is not working. Humm.. it works though but not showing up checked in admin side..

    Here is my code


    Can you help?


  199. Siam Wordpress
    Siam Wordpress at |
  200. WordPress | Annotary
    WordPress | Annotary at |
  201. Osama Felfel
    Osama Felfel Published |

    Perfect !!

  202. Syafaatfm
    Syafaatfm Published |

    Thanks master for the tutorials… You really help me…,, 🙂

  203. crete
    crete Published |

    The very next time I read a blog, I hope that it does not disappoint me just as much as this particular one. I mean, Yes, it was my choice to read, but I really believed you would have something helpful to talk about. All I hear is a bunch of complaining about something that you can fix if you were not too busy searching for attention.

  204. Anthony S
    Anthony S Published |

    Such simple widget, but it requires more than one hundred lines of php code. From your tutorial it is easier to understand how to create a widget than from wordpress help documents.

  205. 38 Hot Excellent WordPress Tutorials For Both Professionals And Beginners | Creative Verse
  206. Creating an widget sur wordpress | WakeupFlower
  207. 38 Hot WP tuts for beginners & pro’s | WordPress Tutorials 101
  208. The complete guide to creating widgets in WordPress 2.8 | Bookmarks
  209. The complete guide to creating widgets in WordPress 2.8 | Bookmarks
  210. Omprakash
    Omprakash Published |

    Great article very descriptive and step by step explanation. Just wanted to know if you are using latest PHP version then you can use __construct instead of making same function name like class. Really great artical 🙂

  211. Naveen
    Naveen Published |

    Hi how to get the textbox in the widget,


    I need to get some input from the visitor of my website and based on the input i need to display something.

    How to achieve the above ?

    Please kindly help me out………!!!

  212. James Bowles
    James Bowles Published |

    How do you go about incorporating nonce into the form submission. When I check the nonce using check_admin_referer() in the update function I get an undefined function error.

  213. Justin D
    Justin D Published |

    Your tuturial doesn’t mention in what folder the files should be located, seems like basic information to me. I need to know in what folder to place the widget.

  214. Martin
    Martin Published |

    Great work, but I think there is a mistake. In the option tag you should use value=”male”, otherwise there are problems with translation. So when you translate this into German “male” would be “männlich” and then ‘male’ == $instance[‘format’] failes, because $instance[‘format’] is “männlich”.



  215. klikharry
    klikharry Published |

    owhh… old article.. wordpress now >> 3,1 version not 2,8 anymore

  216. WordPress Weekend Resources - May 29, 2009
  217. Rahul Kashyap
    Rahul Kashyap Published |

    great tutorial but even i got stock in $new_instance, $old_instance i am totally confused about that 🙁

  218. Random Joe
    Random Joe Published |

    Very good tutorial, explained nicely and all the fields, text, checkbox, select but select and checkbox is not working, I download the plugin and test it in WordPress 3.9.1 any help ?

  219. wordpress教學 | 設計思
    wordpress教學 | 設計思 at |
  220. Sajid anwar
    Sajid anwar Published |

    Fatal error: Call to undefined function get_field_id() in E:\wamp\www\customfield\wp-content\plugins\custom-widget2\update.php on line 6. i am using ajax. when its goto update.php page in response they are said me undefined function get_field_id(). my all code of update.php is this

    include_once($_SERVER[‘DOCUMENT_ROOT’].’/customfield/wp-config.php’ );

    $status = $_GET[‘statu’];

    $test = “asdasdas”;

    echo $ret=”;

    $categories= get_categories();



    $taxonomy_names = get_object_taxonomies($status);

    $taxonomy_name =$taxonomy_names[0];

    $tax_terms = get_terms($taxonomy_name);

    foreach ($tax_terms as $tax_term):

    $option = ‘<option value=”‘ .$tax_term->name . ‘” ‘;

    if ($cat->cat_ID == $categoryid) :

    $option .= ‘ selected = “selected” ‘;


    $option .= ‘”>’;

    $option .= $tax_term->name;

    $option .= ”;


    echo $option;



  221. 27 نکته کاربردی در طراحی سایت و قالب وردپرس - وب سیتی

Comments are closed.