WordPress.org

Ready to get started?Download WordPress

Codex

Plugin API/Action Reference/admin enqueue scripts

Contents

Description

admin_enqueue_scripts is the first action hooked into the admin scripts actions. This hook doesn't provide any parameters, so it can only be used to callback a specified function.

Usage

<?php add_action( 'admin_enqueue_scripts', 'function_name' ); ?>

where "function_name" is the name of the function to be called.

Example: Load CSS File on All Admin Pages

Sometimes you want to load a set of CSS and/or Javascript documents to all admin pages. You can do this from within your plugin or from your themes function file:

function load_custom_wp_admin_style() {
        wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/admin-style.css', false, '1.0.0' );
        wp_enqueue_style( 'custom_wp_admin_css' );
}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );

In this example we are loading a CSS file from the current active "parent" themes directory.

Example: Target a Specific Admin Page

The admin_enqueue_scripts action hook can also be used to target a specific admin page. In this example we are loading a javascript file in the head section of edit.php.

function my_enqueue($hook) {
    if( 'edit.php' != $hook )
        return;
    wp_enqueue_script( 'my_custom_script', plugin_dir_url( __FILE__ ) . 'myscript.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

See also

Resources

Related