wp_update_custom_css_post( string $css, array $args = array() ): WP_Post|WP_Error
- Since
- 4.7.0
- Source
wp-includes/theme.php:2076
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()$preprocessedstringOptional. Pre-processed CSS, stored inpost_content_filtered. Normally empty string.$stylesheetstringOptional. 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
- Scaling
- Constant
- Instructions
- 67–93
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.
Third-party callbacks on 'update_custom_css_data' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_post()called directly - optionoption read or write
get_option()one call below wp_update_custom_css_post() - serializeserialisation
maybe_serialize()one call below wp_update_custom_css_post()
Further down the call graph this can also reach cache 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.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 67 | get_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() | 67 | get_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() | 70 | get_stylesheet(), preprocessed(), compact(), array_merge(), apply_filters(), sanitize_title(), wp_get_custom_css_post(), wp_slash(), wp_update_post(), is_wp_error(), get_post() |
| always | 70 | get_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() |
| always | 80–83 | get_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–86 | get_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() |
| always | 84–87 | get_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() |
| always | 86 | get_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–90 | get_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() | 89 | get_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() |
| always | 90 | get_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() | 93 | get_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:
- apply_filters( update_custom_css_data )filterline 2125 (+49 into the body)
Filters the `css` (`post_content`) and `preprocessed` (`post_content_filtered`) args for a `custom_css` post being updated.
Uses · 13
- wp_parse_args()Merges user defined arguments into defaults array.
- get_stylesheet()Retrieves name of the current stylesheet.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- wp_get_custom_css_post()Fetches the `custom_css` post for a given theme.
- wp_update_post()Updates a post with new post data.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_insert_post()Inserts or updates a post in the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- set_theme_mod()Updates theme modification value for the active theme.
- wp_get_latest_revision_id_and_total_count()Returns the latest revision ID and count of revisions for a post.
- wp_save_post_revision()Creates a revision for the current version of a post.
Show all 13
- get_post()Retrieves post data given a post ID or post object.
Used by · 1
- WP_Customize_Custom_CSS_Setting::update()Store the CSS setting value in the custom_css custom post type for the stylesheet.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.