返回介绍

wp_update_custom_css_post()

发布于 2017-09-11 13:04:07 字数 4984 浏览 1121 评论 0 收藏 0

wp_update_custom_css_post( string $css,  array $args = array() )

Update the custom_css post for a given theme.


description

Inserts a custom_css post when one doesn’t yet exist.


参数

$css

(string) (Required) CSS, stored in post_content.

$args

(array) (Optional) Args.

  • 'preprocessed'
    (string) Pre-processed CSS, stored in post_content_filtered. Normally empty string. Optional.
  • 'stylesheet'
    (string) Stylesheet (child theme) to update. Optional, defaults to current theme/stylesheet.

Default value: array()


返回值

(WP_Post|WP_Error) Post on success, error on failure.


源代码

File: wp-includes/theme.php

function wp_update_custom_css_post( $css, $args = array() ) {
	$args = wp_parse_args( $args, array(
		'preprocessed' => '',
		'stylesheet' => get_stylesheet(),
	) );

	$data = array(
		'css' => $css,
		'preprocessed' => $args['preprocessed'],
	);

	/**
	 * Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
	 *
	 * This filter can be used by plugin that offer CSS pre-processors, to store the original
	 * pre-processed CSS in `post_content_filtered` and then store processed CSS in `post_content`.
	 * When used in this way, the `post_content_filtered` should be supplied as the setting value
	 * instead of `post_content` via a the `customize_value_custom_css` filter, for example:
	 *
	 * <code>
	 * add_filter( 'customize_value_custom_css', function( $value, $setting ) {
	 *     $post = wp_get_custom_css_post( $setting->stylesheet );
	 *     if ( $post && ! empty( $post->post_content_filtered ) ) {
	 *         $css = $post->post_content_filtered;
	 *     }
	 *     return $css;
	 * }, 10, 2 );
	 * </code>
	 *
	 * @since 4.7.0
	 * @param array $data {
	 *     Custom CSS data.
	 *
	 *     @type string $css          CSS stored in `post_content`.
	 *     @type string $preprocessed Pre-processed CSS stored in `post_content_filtered`. Normally empty string.
	 * }
	 * @param array $args {
	 *     The args passed into `wp_update_custom_css_post()` merged with defaults.
	 *
	 *     @type string $css          The original CSS passed in to be updated.
	 *     @type string $preprocessed The original preprocessed CSS passed in to be updated.
	 *     @type string $stylesheet   The stylesheet (theme) being updated.
	 * }
	 */
	$data = apply_filters( 'update_custom_css_data', $data, array_merge( $args, compact( 'css' ) ) );

	$post_data = array(
		'post_title' => $args['stylesheet'],
		'post_name' => sanitize_title( $args['stylesheet'] ),
		'post_type' => 'custom_css',
		'post_status' => 'publish',
		'post_content' => $data['css'],
		'post_content_filtered' => $data['preprocessed'],
	);

	// Update post if it already exists, otherwise create a new one.
	$post = wp_get_custom_css_post( $args['stylesheet'] );
	if ( $post ) {
		$post_data['ID'] = $post->ID;
		$r = wp_update_post( wp_slash( $post_data ), true );
	} else {
		$r = wp_insert_post( wp_slash( $post_data ), true );

		if ( ! is_wp_error( $r ) ) {
			if ( get_stylesheet() === $args['stylesheet'] ) {
				set_theme_mod( 'custom_css_post_id', $r );
			}

			// Trigger creation of a revision. This should be removed once

更新日志

Versiondescription
4.7.0Introduced.

相关函数

Uses

  • wp-includes/theme.php: update_custom_css_data
  • wp-includes/theme.php: wp_get_custom_css_post()
  • wp-includes/theme.php: set_theme_mod()
  • wp-includes/theme.php: get_stylesheet()
  • wp-includes/formatting.php: wp_slash()
  • wp-includes/formatting.php: sanitize_title()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: get_post()
  • wp-includes/revision.php: wp_get_post_revisions()
  • wp-includes/revision.php: wp_save_post_revision()
  • wp-includes/load.php: is_wp_error()
  • Show 9 more uses Hide more uses

Used By

  • wp-includes/customize/class-wp-customize-custom-css-setting.php: WP_Customize_Custom_CSS_Setting::update()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文