WP_REST_Server::add_image_to_index() WordPress Method

The WP_REST_Server::add_image_to_index() method allows you to add an image to the WordPress REST API index. This method accepts two arguments: the image ID and the image URL. The image ID is the number that is assigned to the image when it is uploaded to the WordPress media library. The image URL is the URL of the image file. This method can be used to add an image to the WordPress REST API index when the image is uploaded to the WordPress media library.

WP_REST_Server::add_image_to_index( WP_REST_Response $response, int $image_id, string $type ) #

Exposes an image through the WordPress REST API.


Description

This is used for fetching this information when user has no rights to update settings.


Top ↑

Parameters

$response

(WP_REST_Response)(Required)REST API response.

$image_id

(int)(Required)Image attachment ID.

$type

(string)(Required)Type of Image.


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-server.php

	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,
				)
			);
		}
	}


Top ↑

Changelog

Changelog
VersionDescription
5.9.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.