The is_active_sidebar WordPress PHP Filter checks if a dynamic sidebar is considered “active” by determining if it contains any widgets.
Usage
add_filter('is_active_sidebar', 'your_custom_function', 10, 2); function your_custom_function($is_active_sidebar, $index) { // your custom code here return $is_active_sidebar; }
Parameters
$is_active_sidebar (bool)
– Indicates whether the sidebar should be considered “active” (i.e., if it contains any widgets).$index (int|string)
– The index, name, or ID of the dynamic sidebar.
More information
See WordPress Developer Resources: is_active_sidebar
Examples
Hide sidebar on a specific page
Hide the sidebar on a page with the ID of 42:
add_filter('is_active_sidebar', 'hide_sidebar_on_specific_page', 10, 2); function hide_sidebar_on_specific_page($is_active_sidebar, $index) { if (is_page(42)) { return false; } return $is_active_sidebar; }
Display sidebar only on single posts
Show the sidebar only on single post pages:
add_filter('is_active_sidebar', 'display_sidebar_on_single_posts', 10, 2); function display_sidebar_on_single_posts($is_active_sidebar, $index) { if (!is_single()) { return false; } return $is_active_sidebar; }
Disable sidebar on mobile devices
Disable the sidebar when viewed on mobile devices:
add_filter('is_active_sidebar', 'disable_sidebar_on_mobile', 10, 2); function disable_sidebar_on_mobile($is_active_sidebar, $index) { if (wp_is_mobile()) { return false; } return $is_active_sidebar; }
Show the sidebar only on a specific category
Display the sidebar only for posts in the “News” category:
add_filter('is_active_sidebar', 'show_sidebar_on_news_category', 10, 2); function show_sidebar_on_news_category($is_active_sidebar, $index) { if (!is_category('News')) { return false; } return $is_active_sidebar; }
Show the sidebar only for logged-in users
Display the sidebar only for logged-in users:
add_filter('is_active_sidebar', 'show_sidebar_for_logged_in_users', 10, 2); function show_sidebar_for_logged_in_users($is_active_sidebar, $index) { if (!is_user_logged_in()) { return false; } return $is_active_sidebar; }