WP_REST_Global_Styles_Controller::get_theme_items( WP_REST_Request $request ): WP_REST_Response|WP_Error

In this article

Returns the given theme global styles variations.

Parameters

$requestWP_REST_Requestrequired
The request instance.

Return

WP_REST_Response|WP_Error

Source

public function get_theme_items( $request ) {
	if ( get_stylesheet() !== $request['stylesheet'] ) {
		// This endpoint only supports the active theme for now.
		return new WP_Error(
			'rest_theme_not_found',
			__( 'Theme not found.' ),
			array( 'status' => 404 )
		);
	}

	$response   = array();

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

	$variations = WP_Theme_JSON_Resolver::get_style_variations();
	foreach ( $variations as $variation ) {
		$variation_theme_json = new WP_Theme_JSON( $variation );
		$resolved_theme_uris  = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $variation_theme_json );
		$data                 = rest_ensure_response( $variation );
		if ( ! empty( $resolved_theme_uris ) ) {
			$data->add_links(
				array(
					'https://api.w.org/theme-file' => $resolved_theme_uris,
				)
			);
		}
		$response[] = $this->prepare_response_for_collection( $data );
	}

	return rest_ensure_response( $response );
}

Changelog

VersionDescription
6.6.0Added custom relative theme file URIs to _links for each item.
6.2.0Returns parent theme variations, if they exist.
6.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.