Im creating my own theme. When I have a clean wordpress installation, there are some default widgets in the sidebar (search, category, recent posts etc). I know that I can remove them from the sidebar by adding a widget to that sidebar, but I want them removed in the sidebar by default. Is there a way to do that without disabling the widget (unregister_widget())?
Disable default WordPress widgets in sidebar
![placeholder](https://olatech.pro/community/wp-content/uploads/2023/06/placeholder.jpeg)
This function will disable all widgets:
Now the
true=true
conditional will disable them all the time, while you only want this to happen with a clean install. So you will have to add a different conditional. Which one depends on your actual purpose.You could use
is_active_widget
to test whether only the standard widgets are active.Another option would be to use the
after_switch_theme
hook to make the deactivation only happen when your theme is activated.You could even detect whether the user has visited the widgets page in the backend and decide that after this he apparently is cool with the widgets as they are. This would involve setting an option in the database.
It works for me. Deregister all widgets from sidebar. You have to call it once, for example in after_switch_theme action
EDIT:
https://developer.wordpress.org/reference/functions/wp_get_sidebars_widgets/
“This functionâs access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions”
I have a widget area called “aftercontent” and I wanted to disable the Categories and Annual Archive Widgets when viewing a custom post type called Staff. This is how I did it.
I used “substr( $widget, 0, 10 )” because if I want to remove any widget that begins with “categories”. Else I could have just said “if($widget == ‘categories-3’)” or whatever the exact name of the widget is.