The dynamic_sidebar_has_widgets WordPress PHP filter allows you to modify whether a sidebar has widgets or not.
Usage
add_filter('dynamic_sidebar_has_widgets', 'your_custom_function', 10, 2); function your_custom_function($did_one, $index) { // your custom code here return $did_one; }
Parameters
$did_one
(bool) – Whether at least one widget was rendered in the sidebar. Default is false.$index
(int|string) – Index, name, or ID of the dynamic sidebar.
More information
See WordPress Developer Resources: dynamic_sidebar_has_widgets
Examples
Always display a specific widget in all sidebars
If you want to always display a specific widget in all sidebars, you can use this filter.
add_filter('dynamic_sidebar_has_widgets', 'always_show_widget', 10, 2); function always_show_widget($did_one, $index) { if (!$did_one) { // Display your specific widget the_widget('WP_Widget_YourWidget'); } return true; }
Hide widgets on a specific page
To hide widgets on a specific page, you can use this filter.
add_filter('dynamic_sidebar_has_widgets', 'hide_widgets_on_specific_page', 10, 2); function hide_widgets_on_specific_page($did_one, $index) { if (is_page('your-specific-page-slug')) { return false; } return $did_one; }
Show widgets only for logged-in users
You can use this filter to show widgets only for logged-in users.
add_filter('dynamic_sidebar_has_widgets', 'show_widgets_for_logged_in_users', 10, 2); function show_widgets_for_logged_in_users($did_one, $index) { if (!is_user_logged_in()) { return false; } return $did_one; }
Hide widgets on certain post types
To hide widgets on certain post types, you can use this filter.
add_filter('dynamic_sidebar_has_widgets', 'hide_widgets_on_post_types', 10, 2); function hide_widgets_on_post_types($did_one, $index) { if (is_singular('your-post-type')) { return false; } return $did_one; }
Show widgets only on specific categories
You can use this filter to show widgets only on specific categories.
add_filter('dynamic_sidebar_has_widgets', 'show_widgets_on_specific_categories', 10, 2); function show_widgets_on_specific_categories($did_one, $index) { if (!is_category(array('category-1', 'category-2'))) { return false; } return $did_one; }