Using WordPress ‘is_blog_admin()’ PHP function

The is_blog_admin() WordPress PHP function determines whether the current request is for a site’s administrative interface (e.g. /wp-admin/). This function does not check if the user is an administrator; use current_user_can() for checking roles and capabilities.

Usage

if (is_blog_admin()) {
    // Your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: is_blog_admin()

Examples

Display an admin-only notice

If you want to display a custom notice only for the site’s administrative interface:

function display_admin_notice() {
    if (is_blog_admin()) {
        echo '<div class="notice notice-success is-dismissible">';
        echo '<p>Your custom admin notice here.</p>';
        echo '</div>';
    }
}
add_action('admin_notices', 'display_admin_notice');

Enqueue admin-only styles and scripts

If you want to enqueue styles and scripts only for the site’s administrative interface:

function enqueue_admin_styles_scripts() {
    if (is_blog_admin()) {
        wp_enqueue_style('admin-styles', get_template_directory_uri() . '/admin-styles.css');
        wp_enqueue_script('admin-scripts', get_template_directory_uri() . '/admin-scripts.js', array('jquery'), '', true);
    }
}
add_action('admin_enqueue_scripts', 'enqueue_admin_styles_scripts');

Restrict access to a specific admin page

If you want to restrict access to a specific admin page for non-admin users:

function restrict_admin_page_access() {
    if (is_blog_admin() && !current_user_can('manage_options') && $_GET['page'] === 'restricted_page_slug') {
        wp_die('You do not have sufficient permissions to access this page.');
    }
}
add_action('admin_init', 'restrict_admin_page_access');

Perform action on admin pages only

If you want to perform a custom action only on the site’s administrative interface:

function custom_admin_action() {
    if (is_blog_admin()) {
        // Your custom action code here
    }
}
add_action('init', 'custom_admin_action');

Display custom admin dashboard widget

If you want to display a custom dashboard widget only for the site’s administrative interface:

function add_custom_dashboard_widget() {
    if (is_blog_admin()) {
        wp_add_dashboard_widget('custom_dashboard_widget', 'Custom Dashboard Widget', 'custom_dashboard_widget_content');
    }
}
add_action('wp_dashboard_setup', 'add_custom_dashboard_widget');

function custom_dashboard_widget_content() {
    // Your custom dashboard widget content here
}