Using WordPress ‘customize_panel_active’ PHP filter

The customize_panel_active WordPress PHP filter allows you to modify the response of the WP_Customize_Panel::active() method, which determines if a Customizer panel is active or not.

Usage

add_filter('customize_panel_active', 'your_custom_function', 10, 2);

function your_custom_function($active, $panel) {
  // your custom code here

  return $active;
}

Parameters

  • $active (bool) – Whether the Customizer panel is active.
  • $panel (WP_Customize_Panel) – Instance of the WP_Customize_Panel.

More information

See WordPress Developer Resources: customize_panel_active

Examples

Deactivating a specific Customizer panel

Deactivates the ‘nav_menus’ panel in the Customizer.

add_filter('customize_panel_active', 'deactivate_nav_menus_panel', 10, 2);

function deactivate_nav_menus_panel($active, $panel) {
  if ('nav_menus' === $panel->id) {
    return false;
  }

  return $active;
}

Activating a Customizer panel based on user role

Activates the ‘example_panel’ panel only for users with the ‘editor’ role.

add_filter('customize_panel_active', 'activate_panel_for_editor_role', 10, 2);

function activate_panel_for_editor_role($active, $panel) {
  if ('example_panel' === $panel->id && current_user_can('editor')) {
    return true;
  }

  return $active;
}

Deactivating all Customizer panels

Deactivates all Customizer panels.

add_filter('customize_panel_active', 'deactivate_all_customizer_panels', 10, 2);

function deactivate_all_customizer_panels($active, $panel) {
  return false;
}

Activating a Customizer panel based on a condition

Activates the ‘conditional_panel’ panel only if a specific theme option is set.

add_filter('customize_panel_active', 'activate_panel_based_on_theme_option', 10, 2);

function activate_panel_based_on_theme_option($active, $panel) {
  $theme_option = get_theme_mod('example_theme_option', false);

  if ('conditional_panel' === $panel->id && $theme_option) {
    return true;
  }

  return $active;
}

Deactivating a Customizer panel on specific templates

Deactivates the ‘page_options’ panel on the ‘front-page.php’ template.

add_filter('customize_panel_active', 'deactivate_panel_on_front_page', 10, 2);

function deactivate_panel_on_front_page($active, $panel) {
  if ('page_options' === $panel->id && is_page_template('front-page.php')) {
    return false;
  }

  return $active;
}