WP_REST_Response::add_link() WordPress Method

The WP_REST_Response::add_link() function adds a link to the response. The first parameter is the link relation type (e.g. 'canonical', 'alternate', etc.), the second parameter is the link URL, and the third parameter is an array of link attributes. The function returns the WP_REST_Response object, so it can be chained with other response methods. This function is useful for adding links to the response header, which can be used by the client to request additional information or perform actions. For example, a 'canonical' link can be used to indicate the preferred URL for a resource, or an 'edit' link can be used to provide a URL for editing the resource.

WP_REST_Response::add_link( string $rel, string $href, array $attributes = array() ) #

Adds a link to the response.


Parameters

$rel

(string)(Required)Link relation. Either an IANA registered type, or an absolute URL.

$href

(string)(Required)Target URI for the link.

$attributes

(array)(Optional) Link parameters to send along with the URL.

Default value: array()


Top ↑

Source

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

	public function add_link( $rel, $href, $attributes = array() ) {
		if ( empty( $this->links[ $rel ] ) ) {
			$this->links[ $rel ] = array();
		}

		if ( isset( $attributes['href'] ) ) {
			// Remove the href attribute, as it's used for the main URL.
			unset( $attributes['href'] );
		}

		$this->links[ $rel ][] = array(
			'href'       => $href,
			'attributes' => $attributes,
		);
	}


Top ↑

Changelog

Changelog
VersionDescription
4.4.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.