3 * Dependencies API: Styles functions
8 * @subpackage Dependencies
12 * Initialize $wp_styles if it has not been set.
14 * @global WP_Styles $wp_styles
18 * @return WP_Styles WP_Styles instance.
20 function wp_styles() {
22 if ( ! ( $wp_styles instanceof WP_Styles ) ) {
23 $wp_styles = new WP_Styles();
29 * Display styles that are in the $handles queue.
31 * Passing an empty array to $handles prints the queue,
32 * passing an array with one string prints that style,
33 * and passing an array of strings prints those styles.
35 * @global WP_Styles $wp_styles The WP_Styles object for printing styles.
39 * @param string|bool|array $handles Styles to be printed. Default 'false'.
40 * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
42 function wp_print_styles( $handles = false ) {
43 if ( '' === $handles ) { // for wp_head
47 * Fires before styles in the $handles queue are printed.
52 do_action( 'wp_print_styles' );
55 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
58 if ( ! ( $wp_styles instanceof WP_Styles ) ) {
60 return array(); // No need to instantiate if nothing is there.
64 return wp_styles()->do_items( $handles );
68 * Add extra CSS styles to a registered stylesheet.
70 * Styles will only be added if the stylesheet in already in the queue.
71 * Accepts a string $data containing the CSS. If two or more CSS code blocks
72 * are added to the same stylesheet $handle, they will be printed in the order
73 * they were added, i.e. the latter added styles can redeclare the previous.
75 * @see WP_Styles::add_inline_style()
79 * @param string $handle Name of the stylesheet to add the extra styles to.
80 * @param string $data String containing the CSS styles to be added.
81 * @return bool True on success, false on failure.
83 function wp_add_inline_style( $handle, $data ) {
84 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
86 if ( false !== stripos( $data, '</style>' ) ) {
87 _doing_it_wrong( __FUNCTION__, __( 'Do not pass style tags to wp_add_inline_style().' ), '3.7' );
88 $data = trim( preg_replace( '#<style[^>]*>(.*)</style>#is', '$1', $data ) );
91 return wp_styles()->add_inline_style( $handle, $data );
95 * Register a CSS stylesheet.
97 * @see WP_Dependencies::add()
98 * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
101 * @since 4.3.0 A return value was added.
103 * @param string $handle Name of the stylesheet. Should be unique.
104 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
105 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
106 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
107 * as a query string for cache busting purposes. If version is set to false, a version
108 * number is automatically added equal to current installed WordPress version.
109 * If set to null, no version is added.
110 * @param string $media Optional. The media for which this stylesheet has been defined.
111 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
112 * '(orientation: portrait)' and '(max-width: 640px)'.
113 * @return bool Whether the style has been registered. True on success, false on failure.
115 function wp_register_style( $handle, $src, $deps = array(), $ver = false, $media = 'all' ) {
116 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
118 return wp_styles()->add( $handle, $src, $deps, $ver, $media );
122 * Remove a registered stylesheet.
124 * @see WP_Dependencies::remove()
128 * @param string $handle Name of the stylesheet to be removed.
130 function wp_deregister_style( $handle ) {
131 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
133 wp_styles()->remove( $handle );
137 * Enqueue a CSS stylesheet.
139 * Registers the style if source provided (does NOT overwrite) and enqueues.
141 * @see WP_Dependencies::add()
142 * @see WP_Dependencies::enqueue()
143 * @link http://www.w3.org/TR/CSS2/media.html#media-types List of CSS media types.
147 * @param string $handle Name of the stylesheet. Should be unique.
148 * @param string $src Full URL of the stylesheet, or path of the stylesheet relative to the WordPress root directory.
149 * @param array $deps Optional. An array of registered stylesheet handles this stylesheet depends on. Default empty array.
150 * @param string|bool|null $ver Optional. String specifying stylesheet version number, if it has one, which is added to the URL
151 * as a query string for cache busting purposes. If version is set to false, a version
152 * number is automatically added equal to current installed WordPress version.
153 * If set to null, no version is added.
154 * @param string $media Optional. The media for which this stylesheet has been defined.
155 * Default 'all'. Accepts media types like 'all', 'print' and 'screen', or media queries like
156 * '(orientation: portrait)' and '(max-width: 640px)'.
158 function wp_enqueue_style( $handle, $src = false, $deps = array(), $ver = false, $media = 'all' ) {
159 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
161 $wp_styles = wp_styles();
164 $_handle = explode('?', $handle);
165 $wp_styles->add( $_handle[0], $src, $deps, $ver, $media );
167 $wp_styles->enqueue( $handle );
171 * Remove a previously enqueued CSS stylesheet.
173 * @see WP_Dependencies::dequeue()
177 * @param string $handle Name of the stylesheet to be removed.
179 function wp_dequeue_style( $handle ) {
180 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
182 wp_styles()->dequeue( $handle );
186 * Check whether a CSS stylesheet has been added to the queue.
190 * @param string $handle Name of the stylesheet.
191 * @param string $list Optional. Status of the stylesheet to check. Default 'enqueued'.
192 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
193 * @return bool Whether style is queued.
195 function wp_style_is( $handle, $list = 'enqueued' ) {
196 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
198 return (bool) wp_styles()->query( $handle, $list );
202 * Add metadata to a CSS stylesheet.
204 * Works only if the stylesheet has already been added.
206 * Possible values for $key and $value:
207 * 'conditional' string Comments for IE 6, lte IE 7 etc.
208 * 'rtl' bool|string To declare an RTL stylesheet.
209 * 'suffix' string Optional suffix, used in combination with RTL.
210 * 'alt' bool For rel="alternate stylesheet".
211 * 'title' string For preferred/alternate stylesheets.
213 * @see WP_Dependency::add_data()
217 * @param string $handle Name of the stylesheet.
218 * @param string $key Name of data point for which we're storing a value.
219 * Accepts 'conditional', 'rtl' and 'suffix', 'alt' and 'title'.
220 * @param mixed $value String containing the CSS data to be added.
221 * @return bool True on success, false on failure.
223 function wp_style_add_data( $handle, $key, $value ) {
224 return wp_styles()->add_data( $handle, $key, $value );