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.

About these ads

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

  9. Markd said

    can you make this function “pastable” so it can be copy/paste?

    thanks,

    Mark

  10. The code above is in image format. That is sweet, not being able to copy :)

  11. Ajay said

    Hi Leonid Mamchenkov,

    I git the code how to remove the default widgets from sidebar but is three and way to assign some widgets to sidebar in WPMU when new new blog is created i used the below given code but it is’nt work for me .The code is

    ‘My Categories’ ));

    add_option(“sidebars_widgets”,
    array(“sidebar-1″ => array(“recent-posts”),
    “sidebar-2″ => array(“categories”, “tag_cloud”),
    “sidebar-3″ => array(“pages”, “links”)));

    }
    add_action(‘populate_options’, ‘new_blogs_setting’);

    ?>

    Thank you in advance….. Please fix this or suggest me some other way to do that.

  12. Ajay said

    HI philip Arthur Moore,
    I Need your help in a task that is little complicated to me.

    How to add default widgets in sidebar when i activate the new blog in WPMU.I have tried lots of code as well as function but not able to do that.Please help me in this or suggest me some other way.

    Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 40 other followers

%d bloggers like this: