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:
- You are missing on a lot of fun.
- 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
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.