WordPress Bits

Hacking WordPress. Keeping the bits together.

Disable default WordPress widgets

Posted by Leonid Mamchenkov on June 15, 2007

I am currently building a web site, which uses WordPress as a platform. It’s not a blog, but an extended a very customized content management system (CMS) with several authors and a lot of custom plugins.

One of the things the client requested was maximum flexibility of the front page. They want to have a number of information blocks, such as currency quotes, market analysis, navigation menu, and advertising units; and they want to be able to rearrange them once in a while.

This is, of course, a perfect task for WordPress widgets. If you are not familiar with WordPress widgets, I only have two things to say to you right now:

  1. You are missing on a lot of fun.
  2. Subscribe to RSS feed of this blog, as I’ll be talking a lot about WordPress widgets.

Anyway. I have created all the widgets that they wanted, and I tested them to work properly. But there was a tiny problem. WordPress install comes with a few widgets of its own. Things like archives, blogroll, recent comments, and search box – all have a widget version. But most of these widgets, as good as they are for a regular blog, didn’t make any sense for this web site that I am working on. There are no archives. Calendar has a totally different meaning. And comments are disabled and hidden.

Of course, I could just left the widgets out of the sidebars. But why give user a choice which doesn’t work or makes no sense, right? I wanted them removed. Hidden. Unavailable. And I didn’t want to modify any core WordPress files, so that upgrading this web site to a newer version of WordPress is easier in the future.

Here is my solution to the problem.

In the theme directory, I have a functions.php file (this file has special meaning to WordPress). In this file, I have these lines:

Hide default widgets

Here is what happens. I define a function remove_default_widgets(), which is called for action widgets_init (more on this later, but if you are in a hurry, read file wp-includes/widgets.php around lines 922-958 in WordPress 2.2). This action is called just after the default widgets are registered. In this function I simply unregister all default widgets.

Hopefully, you can spot two exceptions here. Two special widgets are Text widget and RSS widget. These two widgets have additional controls on the administration page. They ask user for options (specifically: how many copies of each of these widgets user wants). Since I don’t want to use these widgets, I don’t need their options too. The options are initialized when sidebar_admin_setup and sidebar_admin_page actions take place. By looking through WordPress source code, I manage to find the appropriate callbacks, which I can now easily cancel.

Done.

8 Responses to “Disable default WordPress widgets”

  1. tawodi said

    in my categories there was a box for each I thought it was really neat looking now they are just blog rolled I noticed it yesterday is this a permanent change or can I have them back the way they were???

  2. tawodi,

    just remove the line, which disables the ‘Categories’ widget:

    unregister_sidebar_widget(’Categories’);

    Your Categories widget will be back and the behavior restored. Nothing is permanently removed. It’s all temporary, for as long as you have the code in place.

  3. Dre said

    What if you just wanted to make the title of the widget unclickable?

  4. Dre,

    I think it either works or not :)
    which I guess is the right way to do UI. Why show something that doesn’t work?

  5. For some reason this doesn’t work with unregister_sidebar_widget('Tag Cloud');. No clue why. It doesn’t appear any different in widgets.php. Otherwise, very cool! Thanks!

  6. Ian,

    maybe it’s something to do with space, or capital letters… Don’t know, really. I haven’t been through WordPress 2.3 code yet…

  7. tom said

    Nice, would have been better if you had posted the code….heehee

  8. An update for anyone else who stumbles upon this post: instead of using nice names for the widget unregistering line, use the ’slugs’ included in the newest versions of WordPress. So, for example, instead of using ‘Recent Comments’ or ‘Recent Posts’, use ‘recent-comments’ and ‘recent-posts’. You will find all of these on lines ~1400+ of /wp-includes/widgets.php (as of WordPress version 2.6.3).

    Additionally, as far as I can tell, there is no need to add in the extra lines of code for the RSS and text blocks. Simply use unregister_sidebar_widget (‘text-1′); and it should work. Cheers.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>