Title: WP_REST_Global_Styles_Controller::prepare_item_for_database
Published: February 3, 2022
Last modified: May 20, 2026

---

# WP_REST_Global_Styles_Controller::prepare_item_for_database( WP_REST_Request $request ): stdClass|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Parameters](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#wp--skip-link--target)

Prepares a single global styles config for update.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#parameters)󠁿

 `$request`[WP_REST_Request](https://developer.wordpress.org/reference/classes/wp_rest_request/)
required

Request object.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#return)󠁿

 stdClass|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
Prepared item on success. [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
on when the custom CSS is not valid.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#source)󠁿

    ```php
    protected function prepare_item_for_database( $request ) {
    	$changes     = new stdClass();
    	$changes->ID = $request['id'];

    	$post            = get_post( $request['id'] );
    	$existing_config = array();
    	if ( $post ) {
    		$existing_config     = json_decode( $post->post_content, true );
    		$json_decoding_error = json_last_error();
    		if ( JSON_ERROR_NONE !== $json_decoding_error || ! isset( $existing_config['isGlobalStylesUserThemeJSON'] ) ||
    			! $existing_config['isGlobalStylesUserThemeJSON'] ) {
    			$existing_config = array();
    		}
    	}

    	if ( isset( $request['styles'] ) || isset( $request['settings'] ) ) {
    		$config = array();
    		if ( isset( $request['styles'] ) ) {
    			if ( isset( $request['styles']['css'] ) ) {
    				$css_validation_result = $this->validate_custom_css( $request['styles']['css'] );
    				if ( is_wp_error( $css_validation_result ) ) {
    					return $css_validation_result;
    				}
    			}
    			$config['styles'] = $request['styles'];
    		} elseif ( isset( $existing_config['styles'] ) ) {
    			$config['styles'] = $existing_config['styles'];
    		}

    		// Register theme-defined variations e.g. from block style variation partials under `/styles`.
    		$variations = WP_Theme_JSON_Resolver::get_style_variations( 'block' );
    		wp_register_block_style_variations_from_theme_json_partials( $variations );

    		if ( isset( $request['settings'] ) ) {
    			$config['settings'] = $request['settings'];
    		} elseif ( isset( $existing_config['settings'] ) ) {
    			$config['settings'] = $existing_config['settings'];
    		}
    		$config['isGlobalStylesUserThemeJSON'] = true;
    		$config['version']                     = WP_Theme_JSON::LATEST_SCHEMA;
    		/**
    		 * JSON encode the data stored in post content.
    		 * Escape characters that are likely to be mangled by HTML filters: "<>&".
    		 *
    		 * This data is later re-encoded by wp_filter_global_styles_post().
    		 * The escaping is also applied here as a precaution.
    		 */
    		$changes->post_content = wp_json_encode( $config, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP );
    	}

    	// Post title.
    	if ( isset( $request['title'] ) ) {
    		if ( is_string( $request['title'] ) ) {
    			$changes->post_title = $request['title'];
    		} elseif ( ! empty( $request['title']['raw'] ) ) {
    			$changes->post_title = $request['title']['raw'];
    		}
    	}

    	return $changes;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php#L238)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php#L238-L298)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_REST_Global_Styles_Controller::validate_custom_css()](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/validate_custom_css/)`wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php` |

Validate style.css as valid CSS.

  | 
| [WP_Theme_JSON_Resolver::get_style_variations()](https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_style_variations/)`wp-includes/class-wp-theme-json-resolver.php` |

Returns the style variations defined by the theme.

  | 
| [wp_json_encode()](https://developer.wordpress.org/reference/functions/wp_json_encode/)`wp-includes/functions.php` |

Encodes a variable into JSON, with some confidence checks.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

[Show 3 more](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_rest_global_styles_controller/prepare_item_for_database/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Added registration of block style variations from theme.json sources (theme.json, user theme.json, partials). | 
| [6.2.0](https://developer.wordpress.org/reference/since/6.2.0/) | Added validation of styles.css property. | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_rest_global_styles_controller%2Fprepare_item_for_database%2F)
before being able to contribute a note or feedback.