function clearwidgets(){
//stuff here only runs once, when the theme is activated for the 1st time
}
register_activation_hook(__FILE__, 'clearwidgets');
And the code I am trying to execute is this:
add_filter( 'sidebars_widgets', 'unset_sidebar_widget' );
function unset_sidebar_widget( $sidebars_widgets ) {
unset( $sidebars_widgets[ '$sidebar_id' ] );
return $sidebars_widgets;
}
That means when the first time the theme is installed it should clear away all the default widget set by WordPress.
Where am I going wrong because the desired result is not achieved? Please suggest me the fix or direct me in the direction so that I can troubleshoot.
register_activation_hook()
is only for plugin activation. To handle activation of themes, use theafter_switch_theme
hook. With regards to widgets, how do you know they're the default? I suggest leaving people's content alone when activating your theme. If you want to show of what your theme can look like, consider using the Starter Content feature: make.wordpress.org/core/2016/11/30/…