WP_REST_Server::get_index( array $request ): WP_REST_Response

Retrieves the site index.

Description

This endpoint describes the capabilities of the site.

Parameters

$requestarrayrequired
Request.
  • context string
    Context.

Return

WP_REST_Response The API root index data.

Source

public function get_index( $request ) {
	// General site data.
	$available = array(
		'name'            => get_option( 'blogname' ),
		'description'     => get_option( 'blogdescription' ),
		'url'             => get_option( 'siteurl' ),
		'home'            => home_url(),
		'gmt_offset'      => get_option( 'gmt_offset' ),
		'timezone_string' => get_option( 'timezone_string' ),
		'namespaces'      => array_keys( $this->namespaces ),
		'authentication'  => array(),
		'routes'          => $this->get_data_for_routes( $this->get_routes(), $request['context'] ),
	);

	$response = new WP_REST_Response( $available );

	$fields = isset( $request['_fields'] ) ? $request['_fields'] : '';
	$fields = wp_parse_list( $fields );
	if ( empty( $fields ) ) {
		$fields[] = '_links';
	}

	if ( $request->has_param( '_embed' ) ) {
		$fields[] = '_embedded';
	}

	if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
		$response->add_link( 'help', 'https://developer.wordpress.org/rest-api/' );
		$this->add_active_theme_link_to_index( $response );
		$this->add_site_logo_to_index( $response );
		$this->add_site_icon_to_index( $response );
	} else {
		if ( rest_is_field_included( 'site_logo', $fields ) ) {
			$this->add_site_logo_to_index( $response );
		}
		if ( rest_is_field_included( 'site_icon', $fields ) || rest_is_field_included( 'site_icon_url', $fields ) ) {
			$this->add_site_icon_to_index( $response );
		}
	}

	/**
	 * Filters the REST API root index data.
	 *
	 * This contains the data describing the API. This includes information
	 * about supported authentication schemes, supported namespaces, routes
	 * available on the API, and a small amount of data about the site.
	 *
	 * @since 4.4.0
	 * @since 6.0.0 Added `$request` parameter.
	 *
	 * @param WP_REST_Response $response Response data.
	 * @param WP_REST_Request  $request  Request data.
	 */
	return apply_filters( 'rest_index', $response, $request );
}

Hooks

apply_filters( ‘rest_index’, WP_REST_Response $response, WP_REST_Request $request )

Filters the REST API root index data.

Changelog

VersionDescription
4.4.0Introduced.

User Contributed Notes

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