Title: WP_REST_Global_Styles_Controller::prepare_item_for_response
Published: February 3, 2022
Last modified: August 20, 2026

---

# WP_REST_Global_Styles_Controller::prepare_item_for_response( WP_Post $post, WP_REST_Request $request ): 󠀁[WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)󠁿

## In this article

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

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

Prepare a global styles config output for response.

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

 `$post`[WP_Post](https://developer.wordpress.org/reference/classes/wp_post/)required

Global Styles post object.

`$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_response/?output_format=md#return)󠁿

 [WP_REST_Response](https://developer.wordpress.org/reference/classes/wp_rest_response/)
Response object.

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

    ```php
    public function prepare_item_for_response( $post, $request ) {
    	$raw_config                       = json_decode( $post->post_content, true );
    	$is_global_styles_user_theme_json = isset( $raw_config['isGlobalStylesUserThemeJSON'] ) && true === $raw_config['isGlobalStylesUserThemeJSON'];
    	$config                           = array();
    	$theme_json                       = null;
    	if ( $is_global_styles_user_theme_json ) {
    		$theme_json = new WP_Theme_JSON( $raw_config, 'custom' );
    		$config     = $theme_json->get_raw_data();
    	}

    	// Base fields for every post.
    	$fields = $this->get_fields_for_response( $request );
    	$data   = array();

    	if ( rest_is_field_included( 'id', $fields ) ) {
    		$data['id'] = $post->ID;
    	}

    	if ( rest_is_field_included( 'title', $fields ) ) {
    		$data['title'] = array();
    	}
    	if ( rest_is_field_included( 'title.raw', $fields ) ) {
    		$data['title']['raw'] = $post->post_title;
    	}
    	if ( rest_is_field_included( 'title.rendered', $fields ) ) {
    		add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
    		add_filter( 'private_title_format', array( $this, 'protected_title_format' ) );

    		$data['title']['rendered'] = get_the_title( $post->ID );

    		remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
    		remove_filter( 'private_title_format', array( $this, 'protected_title_format' ) );
    	}

    	if ( rest_is_field_included( 'settings', $fields ) ) {
    		$data['settings'] = ! empty( $config['settings'] ) && $is_global_styles_user_theme_json ? $config['settings'] : new stdClass();
    	}

    	if ( rest_is_field_included( 'styles', $fields ) ) {
    		$data['styles'] = ! empty( $config['styles'] ) && $is_global_styles_user_theme_json ? $config['styles'] : new stdClass();
    	}

    	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
    	$data    = $this->add_additional_fields_to_object( $data, $request );
    	$data    = $this->filter_response_by_context( $data, $context );

    	// Wrap the data in a response object.
    	$response = rest_ensure_response( $data );

    	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
    		$links = $this->prepare_links( $post->ID );

    		// Only return resolved URIs for get requests to user theme JSON.
    		if ( $theme_json ) {
    			$resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json );
    			if ( ! empty( $resolved_theme_uris ) ) {
    				$links['https://api.w.org/theme-file'] = $resolved_theme_uris;
    			}
    		}

    		$response->add_links( $links );
    		if ( ! empty( $links['self']['href'] ) ) {
    			$actions = $this->get_available_actions( $post, $request );
    			$self    = $links['self']['href'];
    			foreach ( $actions as $rel ) {
    				$response->add_link( $rel, $self );
    			}
    		}
    	}

    	return $response;
    }
    ```

[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.1/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php#L310)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php#L310-L381)

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

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

Resolves relative paths in theme.json styles to theme absolute paths and returns them in an array that can be embedded as the value of `_link` object in REST API responses.

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

Prepares links for the request.

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

Get the link relations available for the post and current user.

  | 
| [WP_Theme_JSON::__construct()](https://developer.wordpress.org/reference/classes/wp_theme_json/__construct/)`wp-includes/class-wp-theme-json.php` |

Constructor.

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

Given an array of fields to include in a response, some of which may be `nested.fields`, determine whether the provided field should be included in the response body.

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

Retrieves the post title.

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

Ensures a REST response is a response object (for consistency).

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

Adds a callback function to a filter hook.

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

Removes a callback function from a filter hook.

  |

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

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

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Added custom relative theme file URIs to `_links`. | 
| [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_response%2F)
before being able to contribute a note or feedback.