WP_REST_Server::add_image_to_index()protectedWP 5.9.0

Exposes an image through the WordPress REST API. This is used for fetching this information when user has no rights to update settings.

{} It's a method of the class: WP_REST_Server{}

No Hooks.

Return

null. Nothing.

Usage

// protected - for code of main (parent) or child class
$result = $this->add_image_to_index( $response, $image_id, $type );
$response(WP_REST_Response) (required)
REST API response.
$image_id(int) (required)
Image attachment ID.
$type(string) (required)
Type of Image.

Changelog

Since 5.9.0 Introduced.

Code of WP_REST_Server::add_image_to_index() WP 6.0

protected function add_image_to_index( WP_REST_Response $response, $image_id, $type ) {
	$response->data[ $type ] = (int) $image_id;
	if ( $image_id ) {
		$response->add_link(
			'https://api.w.org/featuredmedia',
			rest_url( rest_get_route_for_post( $image_id ) ),
			array(
				'embeddable' => true,
				'type'       => $type,
			)
		);
	}
}