Using WordPress ‘is_admin()’ PHP function

The is_admin() WordPress PHP function determines whether the current request is for an administrative interface page.

Usage

if (is_admin()) {
    // This code will run if in the admin area
}

Parameters

  • None

More information

See WordPress Developer Resources: is_admin()

Note: This function does not check if the user is an administrator. Use current_user_can() for checking roles and capabilities. Additionally, is_admin() will return false in the theme customizer view and when using the block editor.

Examples

Displaying a message based on the admin status

if (!is_admin()) {
    echo '<div style="text-align:center">Welcome to our website.</div>';
} else {
    echo '<div style="text-align:center">Welcome to your Admin Panels.</div>';
}

Loading a custom script only on the frontend

function my_enqueue_scripts() {
    if (!is_admin()) {
        wp_enqueue_script('my-custom-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), '1.0', true);
    }
}
add_action('wp_enqueue_scripts', 'my_enqueue_scripts');

Loading a custom stylesheet only in the admin area

function my_admin_styles() {
    if (is_admin()) {
        wp_enqueue_style('my-admin-styles', get_template_directory_uri() . '/css/admin-styles.css');
    }
}
add_action('admin_enqueue_scripts', 'my_admin_styles');

Displaying a message based on admin status and customizer preview

if (!is_admin() && !is_customize_preview()) {
    echo 'You are viewing the theme.';
} else {
    echo 'You are viewing the WordPress Administration Panels or Customizer.';
}

Adding an admin notice

function my_admin_notice() {
    if (is_admin()) {
        echo '<div class="notice notice-warning is-dismissible"><p>Your message goes here.</p></div>';
    }
}
add_action('admin_notices', 'my_admin_notice');