wppaste
WordPress

wp_update_custom_css_post( string $css, array $args = array() ): WP_Post|WP_Error

Since
4.7.0
Source
wp-includes/theme.php:2085
Updates the custom_css post for a given theme.

Description

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

Compatibility

WordPress
since 4.7.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$cssstring
CSS, stored in post_content.
$argsarrayoptional
Args.Default: array()
  • $preprocessedstring

    Optional. Pre-processed CSS, stored in post_content_filtered. Normally empty string.
  • $stylesheetstring

    Optional. Stylesheet (child theme) to update. Defaults to active theme/stylesheet.

Return value

WP_Post|WP_Error
Post on success, error on failure.

Performance profile

How much work a call to wp_update_custom_css_post() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
67–93

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
1 hook

Third-party callbacks on 'update_custom_css_data' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()called directly
  • optionoption read or writeget_option()one call below wp_update_custom_css_post()

Further down the call graph this can also reach cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 12 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_update_custom_css_post() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
is_wp_error()67get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_update_post(), is_wp_error()
is_wp_error()67get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), is_wp_error()
!is_wp_error()70get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_update_post(), is_wp_error(), get_post()
always70get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), is_wp_error(), get_post()
always80–83get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), is_wp_error()
!is_wp_error()83–86get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), is_wp_error(), get_post()
always84–87get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), set_theme_mod(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), is_wp_error()
always86get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), wp_save_post_revision(), is_wp_error()
!is_wp_error()87–90get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), set_theme_mod(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), is_wp_error(), get_post()
!is_wp_error()89get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), wp_save_post_revision(), is_wp_error(), get_post()
always90get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), set_theme_mod(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), wp_save_post_revision(), is_wp_error()
!is_wp_error()93get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_insert_post(), is_wp_error(), get_stylesheet(), set_theme_mod(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), wp_save_post_revision(), is_wp_error(), get_post()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 106 instructions, 67–93 executed per call, 6 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_update_custom_css_post() runs, in this order:

  1. apply_filters( update_custom_css_data )filterline 2134 (+49 into the body)

    Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.

Uses · 13

Show all 13
  • get_post()Retrieves post data given a post ID or post object.

Used by · 1

Source code

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 #30854 is resolved.			$revisions = wp_get_latest_revision_id_and_total_count( $r );			if ( ! is_wp_error( $revisions ) && 0 === $revisions['count'] ) {				wp_save_post_revision( $r );			}		}	}

Changelog

Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/theme.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.