wp_add_inline_style()添加一个style.css文件

目录

描述

Adds extra CSS.

Works only if the stylesheet has already been added. Accepts a string $data containing the CSS. If two or more CSS code blocks are added to the same stylesheet $handle, they will be printed in the order they were added, i.e. the latter added styles can redeclare the previous.

wp_add_inline_style() 描述

用法

<?php wp_add_inline_style$handle$data ); ?>

wp_add_inline_style() 用法

参数

$handle

(string) (必填) Name of the script to which to add the extra styles. Lowercase string.

默认值: None

$data

(string) (必填) String containing the CSS to be added.

默认值: None

wp_add_inline_style() 参数

示例

wp_add_inline_style allows you to print extra styling whenever a certain stylesheet is loaded. For instance suppose a plug-in or theme makes use of the class .mycolor in a stylesheet to set a background color. This can be over-ridden by a user chosen color, stored in the database by using wp_add_inline_style to print the extra styling.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
function my_styles_method() {
	wp_enqueue_style(
		'custom-style',
		get_template_directory_uri() . '/css/custom_script.css'
	);
        $color = get_theme_mod( 'my-custom-color' ); //E.g. #FF0000
        $custom_css = "
                .mycolor{
                        background: {$color};
                }";
        wp_add_inline_style( 'custom-style', $custom_css );
}
add_action( 'wp_enqueue_scripts', 'my_styles_method' );
?>

wp_add_inline_style() 示例

历史

添加于 版本: 3.3

wp_add_inline_style() 历史

源文件

wp_add_inline_style() 函数的代码位于 wp-includes/functions.wp-styles.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Add extra CSS styles to a registered stylesheet.
 *
 * Styles will only be added if the stylesheet in already in the queue.
 * Accepts a string $data containing the CSS. If two or more CSS code blocks
 * are added to the same stylesheet $handle, they will be printed in the order
 * they were added, i.e. the latter added styles can redeclare the previous.
 *
 * @see WP_Styles::add_inline_style()
 *
 * @since 3.3.0
 *
 * @param string $handle Name of the stylesheet to add the extra styles to. Must be lowercase.
 * @param string $data   String containing the CSS styles to be added.
 * @return bool True on success, false on failure.
 */
function wp_add_inline_style( $handle, $data ) {
	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );

	if ( false !== stripos( $data, '' ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Do not pass style tags to wp_add_inline_style().' ), '3.7' );
		$data = trim( preg_replace( '#]*>(.*)#is', '$1', $data ) );
	}

	return wp_styles()->add_inline_style( $handle, $data );
}

wp_add_inline_style() 源文件