Using WordPress ‘admin_enqueue_scripts’ PHP action

The admin_enqueue_scripts WordPress PHP action is used to enqueue scripts and styles for all admin pages.

Usage

add_action('admin_enqueue_scripts', 'your_function_name');

function your_function_name($hook_suffix) {
    // your custom code here
    return $hook_suffix;
}

Parameters

  • $hook_suffix (string) – The current admin page.

More information

See WordPress Developer Resources: admin_enqueue_scripts

Examples

Enqueue a custom admin stylesheet

Enqueue a custom stylesheet for the admin dashboard.

add_action('admin_enqueue_scripts', 'enqueue_admin_stylesheet');

function enqueue_admin_stylesheet($hook_suffix) {
    wp_enqueue_style('custom-admin-style', get_template_directory_uri() . '/admin-style.css');
}

Enqueue a script only on the post edit screen

Enqueue a custom script only when editing posts.

add_action('admin_enqueue_scripts', 'enqueue_post_edit_script');

function enqueue_post_edit_script($hook_suffix) {
    if ('post.php' == $hook_suffix) {
        wp_enqueue_script('custom-post-edit-script', get_template_directory_uri() . '/post-edit-script.js', array('jquery'), '1.0.0', true);
    }
}

Enqueue a script only on the theme options page

Enqueue a custom script only on the theme options page.

add_action('admin_enqueue_scripts', 'enqueue_theme_options_script');

function enqueue_theme_options_script($hook_suffix) {
    if ('appearance_page_theme_options' == $hook_suffix) {
        wp_enqueue_script('custom-theme-options-script', get_template_directory_uri() . '/theme-options-script.js', array('jquery'), '1.0.0', true);
    }
}

Enqueue a script only on the custom post type edit screen

Enqueue a custom script only when editing a custom post type.

add_action('admin_enqueue_scripts', 'enqueue_custom_post_type_script');

function enqueue_custom_post_type_script($hook_suffix) {
    global $post_type;
    if ('edit.php' == $hook_suffix && 'your_custom_post_type' == $post_type) {
        wp_enqueue_script('custom-cpt-script', get_template_directory_uri() . '/cpt-script.js', array('jquery'), '1.0.0', true);
    }
}

Enqueue a script only on the widgets page

Enqueue a custom script only on the widgets admin page.

add_action('admin_enqueue_scripts', 'enqueue_widgets_page_script');

function enqueue_widgets_page_script($hook_suffix) {
    if ('widgets.php' == $hook_suffix) {
        wp_enqueue_script('custom-widgets-script', get_template_directory_uri() . '/widgets-script.js', array('jquery'), '1.0.0', true);
    }
}