WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp register style

Description

A safe way to register a CSS style file for later use with wp_enqueue_style().

Usage

<?php wp_register_style$handle$src$deps$ver$media ); ?> Use the wp_enqueue_scripts action to call this function. Calling it outside of an action can lead to problems. See #17916 for details.

Parameters

$handle
(string) (required) Name of the stylesheet (which should be unique as it is used to identify the script in the whole system).
Default: None
$src
(string) (required) URL to the stylesheet. Example: 'http://example.com/css/mystyle.css'. You should never hardcode URLs to local styles, use plugins_url()(for Plugins) and get_template_directory_uri() (for Themes) to get a proper URL. Remote assets can be specified with a protocol-agnostic URL, i.e. '//otherdomain.com/css/theirstyle.css'.
Default: None
$deps
(array) (optional) Array of handles of any stylesheets that this stylesheet depends on. Dependent stylesheets will be loaded before this stylesheet.
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. The version is appended to the stylesheet URL as a query string, such as ?ver=3.5.1. By default, or if false, the WordPress version string is used. If null nothing is appended to the URL.
Default: false
$media
(string) (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: 'all'

Return Values

(bool)
Whether the style has been registered. True on success, false on failure.

Examples

In a Plugin (outside a PHP class)

// Register style sheet.
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );

/**
 * Register style sheet.
 */
function register_plugin_styles() {
	wp_register_style( 'my-plugin', plugins_url( 'my-plugin/css/plugin.css' ) );
	wp_enqueue_style( 'my-plugin' );
}
  • Assumes the Plugin directory is named 'my-plugin'.
  • Assumes the Plugin style sheet is named 'plugin.css'.

In a Plugin (inside a PHP class)

class my_plugin {

	/**
	 * @TODO Add class constructor description.
	 */
	function __construct() {
		// Register style sheet.
		add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
	}

	/**
	 * Register and enqueue style sheet.
	 */
	public function register_plugin_styles() {
		wp_register_style( 'my-plugin', plugins_url( 'my-plugin/css/plugin.css' ) );
		wp_enqueue_style( 'my-plugin' );
	}
}
  • Assumes the Plugin class name is 'my_plugin'.
  • Assumes the Plugin directory is named 'my-plugin'.
  • Assumes the Plugin style sheet is named 'plugin.css'.

Notes

Change Log

  • Since: 2.1 (BackPress version: r79)

Source File

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

Resources

Related

Enqueue Styles

Enqueue Scripts

Front-End Hooks

Admin Hooks

Login Hooks

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