145 responses to “The complete guide to creating widgets in WordPress 2.8”

  1. The Frosty @WPCult

    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

    [...] Justin Tadlock has a nice tutorial on the new way to create WordPress Widgets in WP 2.8. You might remember that in the upcoming version of WordPress the admin interface for Widgets has been revamped, but the method of developing Widgets has been updated as well. [...]

  3. Stefano

    As usual, Justin: great great article!

    Stefano

  4. BuddyPress Links #12 · Buddypress Links

    [...] Completeguide to creating widgets in WordPress [...]

  5. Dicas, Dicas e mais Dicas | pBlog

    [...] Guia para Criar Widgets – tutorial detalhado sobre a criação de widgets para o WordPress 2.8 (em inglês). [...]

  6. J Mehmett

    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

    [...] original here:  The complete guide to creating widgets in WordPress 2.8 You May Like Linking To UsHello and welcome to our blog, you may consider linking to us, because [...]

  8. Good Guide for creating Widgets

    [...] by WP Greet BoxJustin Tadlock wrote a good tutuorial on how to write widgets in Wordpress 2.8. Go here now to check it [...]

  9. Randa Clay

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

  10. Matt

    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

    [...] Tadlock has three posts on WordPress 2.8 already – The Complete Guide to Creating Widgets in 2.8, Custom Taxonomies in WordPress 2.8, and Tag Descriptions in WordPress [...]

  12. Matt

    @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

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

  14. links for 2009-06-03 « sySolution

    [...] The complete guide to creating widgets in WordPress 2.8 (tags: wordpress) [...]

  15. chantelle tamaris

    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

    [...] to create fake registered sidebars anymore. Just use the_widget template tag. This means you can create a custom widget and insert it anywhere you want. The new widget treatment makes an old dream of some WordPress [...]

  17. leon

    Hi
    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
    Leon

  18. The complete guide to creating widgets in WordPress 2.8 | Totally WordPress

    [...] That’s why Justin Tadlock has written “The complete guide to creating widgets in WordPress 2.8″. [...]

  19. WordPress 2.8: Features and tips — gunnerpress.com

    [...] The complete guide to creating widgets in WordPress 2.8 [...]

  20. Wordpress 2.8 And 10 Things That You Should Know Before/After You Upgrade

    [...] Guide to create Widgets in Wordpress 2.8 [...]

  21. Jennifer

    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

    [...] 2. The Complete Guide To Creating Widgets in WordPress2.8 – Justin Tadlock [...]

  23. Jennifer

    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

    Excellent and easy tutorial. Thanks a lot!

  25. Mikko

    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

    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 -

    [...] 2. the complete guide to creating widgets in wordpress2.8 – justin tadlock [...]

  28. Max

    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

    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

    Thanks

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

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

  31. Jon Pruett

    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

    [...] Widgets work – including a easier to use API. In light of this, I made use of the new API (armed with this great tutorial and sample file) and created some custom widgets for my [...]

  33. Mark

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

    Cheers!

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

  35. Whirled Wind » Blog Archive » My First Wordpress Widget

    [...] I wanted to have for the BookMooch widget. But after searching around a little bit I found another widget template over at Justin Tadlock’s site. By taking the plugin control structure I’d already [...]

  36. Publikační systémy – WordPress 2.8 | Phirebase

    [...] 2.8 Resources For Developers | W3Avenue The complete guide to creating widgets in WordPress 2.8 Wordpress 2.8 And 10 Things That You Should Know Before/After You Upgrade 10 WordPress 2.8 Features [...]

  37. Edgar Doiron

    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

    [...] 2. the complete guide to creating widgets in wordpress2.8 – justin tadlock [...]

  39. Create a posts by category widget in WordPress 2.8 | Your Site is Valid Blog

    [...] Tadlock wrote a very comprehensive article about  making widget in 2.8 and I will use this to make a widget that displays only posts from a [...]

  40. P.Raman

    Hi

    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

    [...] The complete guide to creating widgets in WordPress 2.8 (tags: wordpress widgets howto widget development reference tutorial) [...]

  42. WordPressMU 2.8.1 now out, features and a couple bugs - WPMU Tutorials

    [...] still work, but you no longer have to specify them for multiple use. They all are now. Check out the complete guide to creating a widget from Justin [...]

  43. Stéphane Gallay

    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

    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

    @Justin -
    http://coffeeman.name/wp_test/
    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

    [...] 11.The complete guide to creating widgets in WordPress 2.8 [...]

  47. Johnny Goodperson

    Thanks for your tutor mate :)
    i’ll make it :D

  48. Today’s Delicious links - 27/05 | Links | WereWP

    [...] The complete guide to creating widgets in WordPress 2.8 [...]

  49. New And Comprehensive Wordpress 2.8 Tutorial and Hack Toolbox - Programming Blog

    [...] 11.The complete guide to creating widgets in WordPress 2.8 [...]

  50. Joe Random

    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

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

  52. Kevin Baker

    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.

    Kevin

  53. JNFerree

    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

    [...] The complete guide to creating widgets in WordPress 2.8 (tags: wordpress tutorial webdevelopment widgets wordpress_widgets) [...]

  55. Joe Somebody

    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

    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

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

  59. Jane Doe

    Great and complete tutorial Justin,like always thank you!

  60. GTRway

    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

    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

    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

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

  64. Adz

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

    -Adz

  65. Gaurav

    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 :
    http://www.drivehq.com/file/DF.aspx/376855287.gif?isGallery=&share=&shareID=0&fileID=376855287

    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
    continue;

    I can’t touch the wordpress core code.

    Kindly help..

    Thanks,
    Gaurav

  66. Gaurav

    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

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

  68. Sarah

    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

    [...] The complete guide to creating widgets in WordPress 2.8 – An excellent article from Justin Tadlock, a WP Demi-God. Related Posts:Links for December 15thLinks for July 17thLinks for June 16thLinks for August 4thLinks for February 9th Share This: [...]

  70. Create a Social Links Widget in WordPress 2.8

    [...] reading Justin Tadlock’s tutorial: The complete guide to creating widgets for WordPress, I have created eleven custom WordPress 2.8 widgets. It’s actually quite simple, much easier [...]

  71. Trabajo

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

  72. Webdesigner

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

  73. willsy

    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

    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

    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

    [...] The complete guide to creating widgets in 2.8, Justin Tadlock [...]

  77. 10 Most Helpful WordPress Design Blogs | Cosmos Blog -- Internet News,Life,Culture,Polices,Resource,Make Money

    [...] The complete guide to creating widgets in WordPress 2.8 [...]

  78. Len

    Hi,

    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?

    Thanks

  79. Dustin Dempsey

    @ 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

    This is great. Specially very good tutorial for me.
    Thanks….

  81. Ten Tips for Becoming a Better WordPress Developer | Web Design Ledger

    [...] For more information on how you can write your own WordPress widget, Justin Tadlock wrote up an awesome article on the subject. [...]

  82. Tutorial: Widget selbst erstellen (Wordp… | Wordpress Lesezeichen

    [...] Widget selbst erstellen (Wordpress 2.8) http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-WordPress-28 Tweet This!Share this on TechnoratiSubmit this to NetvibesAdd this to Mister WongMark this on [...]

  83. CHRISTMAS 2009 − Links for 2009-11-10 [del.icio.us]

    [...] The complete guide to creating widgets in WordPress 2.8 have seen some good tutorials on creating widgets for WordPress 2.8 floating around the WordPress-o-Sphere. But, I didn’t feel any of them really covered practical usage. I want to show you how to create a widget for real-world development using WordPress 2.8’s new widget class. [...]

  84. elliot condon

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

  85. amber

    your download link appears to be broken.

  86. neha

    hello,

    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?

    Thanks!

  87. Tom

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

  88. beep

    clear explanation…Had some issues but all ok now

    regards

  89. andre

    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!

    http://www.soakingplanet.com

    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!

    Andre

  90. Sébastien Méric

    Hi,

    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.

    Seb.

  91. CoreBloggers

    Wordpress Featured Tag Widget Plugin…

    Wordpress featured tag widget is a wordpress plugin that allows you to add posts on the sidebar of your blog for a particular tag. This plugin is very easy to use and gives many options so that you can configure this plugin according to your requiremen…

  92. Rick Falkvinge

    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

    Great guide Justin ! Thanks !

  94. Brian

    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

    [...] wordpress widget creation tutorial I could [...]

  96. jommy » The complete guide to creating widgets in WordPress 2.8

    [...] The complete guide to creating widgets in WordPress 2.8. [...]

  97. Event Timer Widget for Wordpress | Mark Hedley

    [...] by Justin Tadlock who also gives a resonable explantion for developing one. You can access the page here. The interface is very simple, once it has been uploaded to your plugins directory and activated. [...]

  98. Rupak

    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

    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

    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

    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

    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

      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/

      Cheers,
      frq.

  103. frq

    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'];

    to:

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

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

    to:

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

    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

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

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

    to:

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

    frq.

  105. Bookmarks for February 16th from 22:43 to 22:43 | Brewerj Web Design

    [...] The complete guide to creating widgets in WordPress 2.8 – Our widget will display a person’s name and sex. The controls will allow for the input of a widget title (text input), the input of the user’s name (text input), the selection of the person’s sex (select box), and whether the sex should be shown publicly (checkbox). [...]

  106. 11 Most Common Wordpress Posts | New 2 Wp

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

  107. dandy

    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 ?

    Thanks.

  108. dandy

    i fixed it by replacing
    $instance['format']

    with
    $instance['sex'] in selectbox drop down.

    Thanks.

  109. dandy

    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.

    Thanks.

  110. Owen Hoskins

    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.

    Best,
    Owen

  111. Lee

    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

    [...] I hadn't created any widgets since the new widget API was released in WordPress 2.8, but a great article by Justin Tadlock set me on the right track. I added some customization options for how the sleep count is displayed, [...]

  113. Joey Novak

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

    Thanks!

  114. Johnathan Andersen

    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

    Hi,

    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

    Hello,

    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.

    Thanks,

    Carly

  117. Yvonne

    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

    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) {
        extract($args);
        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');
    	dbDelta($sql);
       }
    }
    
    deepwoods_make_GAN_table();
  119. Robert Heller

    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

      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

    [...] thanks to Justin Tadlock for his great tutorial on how to write widgets for [...]

  121. WP Widget: Load posts in a category

    [...] (of which the current WP theme is built on), has written a meticulous and beautiful tutorial on how to create a widget from scratch for WP2.8 and above. His tutorial is straight-forward and simple – more importantly, he provided a [...]

  122. David Siegfried

    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

    [...] The complete guide to creating widgets in WordPress 2.8. Share and [...]

  124. Ken Kennedy

    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

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

  126. Dineshkumar

    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

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

    1. Eleven

      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

    thanks very helpful, just created my own little widget.

  129. Gaurav

    I m also getting this type of error

    Call to undefined function add_action()

    please give me any solution

  130. Elio

    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

    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

    [...] The complete guide to creating widgets in WordPress 2.8 (tagged: todo WordPress widgets PHP plugin ) [...]

  133. Sebastian

    Awesome guide! Thanks a lot!

  134. Josh

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

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.