1
votes

I am trying to setup some default widgets areas and add to them some default widgets when my theme is activated.

On a fresh WordPress install I got some widgets in the first sidebar
( search, recent posts, archives, meta area),
I am using unregister_widget() to remove those widgets,
now I cannot find a way to add widgets to those sidebars via functions.php.

Could you help me, please?

Thanks for now.

1
I disagree that this is a duplicate.s_ha_dum

1 Answers

1
votes

unregister_widget is going to completely remove the widget from the system making it unavailable for use. What you need to be doing is removing/adding widgets to the sidebar, not unregistering them. There is a good post on the WordPress specific forum: https://wordpress.stackexchange.com/questions/26557/programmatically-add-widgets-to-sidebars

The basic idea is to manipulate the widgets array stored in the options table.

$widgets = get_option( 'sidebars_widgets' );
var_dump($widgets);

If you look at that var_dump it should be pretty obvious what is happening. The tricky part is that all the widgets have numeric suffixes so that multiples can be used.