Using WordPress ‘is_customize_preview()’ PHP function

The is_customize_preview() WordPress PHP function checks if the site is being previewed in the Customizer.

Usage

if (is_customize_preview()) {
  // perform customizer specific action
} else {
  // perform separate action
}

Parameters

  • None

More information

See WordPress Developer Resources: is_customize_preview()

Examples

Show a message only in Customizer preview

Display a message on the site only when it is being previewed in the Customizer.

if (is_customize_preview()) {
  echo 'You are previewing this site in the Customizer.';
}

Enqueue Customizer-specific CSS

Enqueue a CSS file only for Customizer preview.

function my_theme_enqueue_customizer_css() {
  if (is_customize_preview()) {
    wp_enqueue_style('customizer-css', get_template_directory_uri() . '/customizer.css');
  }
}
add_action('wp_enqueue_scripts', 'my_theme_enqueue_customizer_css');

Add Customizer-specific body class

Add a body class only when the site is being previewed in the Customizer.

function my_theme_add_customizer_body_class($classes) {
  if (is_customize_preview()) {
    $classes[] = 'customizer-preview';
  }
  return $classes;
}
add_filter('body_class', 'my_theme_add_customizer_body_class');

Show admin bar only in Customizer preview

Display the admin bar only when the site is being previewed in the Customizer.

function my_theme_show_admin_bar() {
  return is_customize_preview();
}
add_filter('show_admin_bar', 'my_theme_show_admin_bar');

Remove specific action for Customizer preview

Remove a specific action only when the site is being previewed in the Customizer.

function my_theme_remove_action_for_customizer() {
  if (is_customize_preview()) {
    remove_action('some_action', 'some_callback_function');
  }
}
add_action('init', 'my_theme_remove_action_for_customizer');