I’ve been working on a little WordPress project these last few weeks with a colleague. When I first heard the idea, I thought, “That should be easy enough to build.” I decided the best way to tackle the feature would be a custom widget, but when I finally sat down to get it working I quickly began to realize that there were a few obstacles to overcome.
The project was really meant to extend an existing widget, and it seemed confusing to have both the default and extended version there side by side (plus it would be really nice to call them the same thing for transparency). I needed to get rid of it. You can’t just overwrite an existing widget by registering a new one by the same name – WordPress ignores it. And if I did manage to find a way to remove a default widget, when do you have to do it?
Luckily, some digging around found a solution.
1 2 3 4 5 6 7 8 9 10 | function gdm_widget_meta_register() { // unregister the widget and its control wp_unregister_sidebar_widget('meta'); // register the new and improved widget and control wp_register_sidebar_widget('meta', __('Meta'), 'gdm_widget_meta'); wp_register_widget_control('meta', __('Meta'), 'gdm_widget_meta_control'); } add_action('widgets_init', 'gdm_widget_meta_register', 1); |
The really important pieces are the wp_unregister_sidebar_widget function and the widgets_init action hook. widgets_init actions are run right after all the default widgets are registered, giving us the perfect opportunity to call wp_unregister_sidebar_widget with the ID of the widget you’re getting rid of. Now you can register your own widget and control, effectively replacing a default WordPress widget.


May 25, 2009
Спасибо, хорошая статья. Подписался.
May 27, 2009
Честно говоря, сначала до конца не понял, но со второго раза дошло – спасибо!
May 29, 2009
This is one good looking, sexy theme, love it! What theme are you using?
May 30, 2009
Спасибо, интересно было прочитать.
September 18, 2009
Nowadays WP is replacing many good plugins needed in the past.
October 13, 2009
There is no plan nor capital for a so-called electric economy. ,
October 22, 2009
This total includes 52 new measures. ,
October 23, 2009
But a casual fling might be just what she needs to take her mind off bridezillas. ,
December 30, 2009
Very interesting stuff about WordPress Development: Replacing Default Widgets | Garrett Murphey . Thanks and keep up the good work. Merry christmas to u all