Codex

Function Reference/wp enqueue style

Contents

Description

Enqueue a CSS style file.

Usage

<?php wp_enqueue_style$handle$src$deps$ver$media ?>

Parameters

$handle
(string) (required) Name of the stylesheet.
Default: None
$src
(string|boolean) (optional) Path to the stylesheet from the root directory of WordPress. Example: '/css/mystyle.css'.
Default: false
$deps
(array) (optional) Array of handles of any stylesheet that this stylesheet depends on; stylesheets that must be loaded before this stylesheet. false if there are no dependencies.
Default: array()
$ver
(string|boolean) (optional) String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet.
Default: false
$media
(string|boolean) (optional) String specifying the media for which this stylesheet has been defined. Examples: 'all', 'screen', 'handheld', 'print'. See this list for the full range of valid CSS-media-types.
Default: false

Return Values

(void) 
This function does not return a value.

Examples

Using a Hook

<?php

    /*
     * This example will work with WordPress 2.7
     */

    /*
     * register with hook 'wp_print_styles'
     */
    add_action('wp_print_styles', 'add_my_stylesheet');

    /*
     * Enqueue style-file, if it exists.
     */

    function add_my_stylesheet() {
        $myStyleUrl = WP_PLUGIN_URL . '/myPlugin/style.css';
        $myStyleFile = WP_PLUGIN_DIR . '/myPlugin/style.css';
        if ( file_exists($myStyleFile) ) {
            wp_register_style('myStyleSheets', $myStyleUrl);
            wp_enqueue_style( 'myStyleSheets');
        }
    }

?>

Load stylesheets only on plugin pages

<?php

    /*
     * This example will work at least on WordPress 2.6.3, 
     * but maybe on older versions too.
     */

    add_action('admin_init', 'my_plugin_admin_init');
    add_action('admin_menu', 'my_plugin_admin_menu');
    
    function my_plugin_admin_init()
    {
        /* Register our stylesheet. */
        wp_register_style('myPluginStylesheet', WP_PLUGIN_URL . '/myPlugin/stylesheet.css');
    }
    
    function my_plugin_admin_menu()
    {
        /* Register our plugin page */
        $page = add_submenu_page( 'edit.php', 
                                  __('My Plugin', 'myPlugin'), 
                                  __('My Plugin', 'myPlugin'), 9,  __FILE__, 
                                  'my_plugin_manage_menu');
   
        /* Using registered $page handle to hook stylesheet loading */
        add_action('admin_print_styles-' . $page, 'my_plugin_admin_styles');
    }
    
    function my_plugin_admin_styles()
    {
        /*
         * It will be called only on your plugin admin page, enqueue our stylesheet here
         */
        wp_enqueue_style('myPluginStylesheet');
    }
    
    function my_plugin_manage_menu()
    {
        /* Output our admin page */
    }
    
?>

Notes

  • See WP_Styles::add(), WP_Styles::enqueue()
  • Uses global: (unknown type) $wp_styles

Change Log

Since: 2.1 (BackPress version: r79)

Source File

wp_enqueue_style() is located in wp-includes/functions.wp-styles.php.

Related

See also index of Function Reference and index of Template Tags.