wp_custom_css_cb()
概述
wp_custom_css_cb() 函数用于渲染自定义 CSS 样式元素,通常在主题或插件中调用以输出用户保存的 CSS 代码。它检查是否有自定义 CSS 或是否在 Customizer 预览中,并生成相应的 HTML 样式标签。
关键要点
- 函数 wp_custom_css_cb() 渲染自定义 CSS 样式元素,基于 wp_get_custom_css() 获取内容。
- 在自定义 CSS 存在或处于 Customizer 预览模式时输出样式标签,支持 HTML5 类型属性检查。
- 相关函数包括 wp_get_custom_css()、is_customize_preview() 和 current_theme_supports(),用于获取 CSS、检测预览和检查主题支持。
- 自 WordPress 4.7.0 版本引入,是主题开发中处理自定义 CSS 的标准方法。
Renders the Custom CSS style element.
Source
function wp_custom_css_cb() {
$styles = wp_get_custom_css();
if ( $styles || is_customize_preview() ) :
$type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"';
?>
<style<?php echo $type_attr; ?> id="wp-custom-css">
</style>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-includes/theme.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/theme.php#L1968">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/theme.php#L1968-L1981">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_get_custom_css/">wp_get_custom_css()</a><code>wp-includes/theme.php
Fetches the saved Custom CSS content for rendering.
is_customize_preview()wp-includes/theme.php
Whether the site is being previewed in the Customizer.
current_theme_supports()wp-includes/theme.php
Checks a theme’s support for a given feature.
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |