wppaste
WordPress

WP_REST_Server::embed_links( array $data, bool|string[] $embed = true ): array

Since
4.4.0, 5.4.0
Source
wp-includes/rest-api/class-wp-rest-server.php:775
Embeds the links from the data into the request.

Compatibility

WordPress
since 5.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$dataarray
Data from the request.
$embedbool|string[]optional
Whether to embed all links or a filtered list of link relations.
Default true.Default: true

Return value

array
Data with sub-requests embedded.
  • $_linksarray

    Links.
  • $_embeddedarray

    Embedded objects.

Performance profile

How much work a call to WP_REST_Server::embed_links() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–14

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 107.

Plugin surface
1 hook

Third-party callbacks on 'rest_post_dispatch' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Server::embed_links() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–14none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1075–1416
8.51075–1416
8.41075–14163 fewer instructions than PHP 8.3
8.31105–1416
8.21105–1416
8.11105–1416
7.41105–1416

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_REST_Server::embed_links() runs, in this order:

  1. apply_filters( rest_post_dispatch )filterline 824 (+49 into the body)

    Filters the REST API response.

Uses · 7

Used by · 1

Source code

	protected function embed_links( $data, $embed = true ) {		if ( empty( $data['_links'] ) ) {			return $data;		} 		$embedded = array(); 		foreach ( $data['_links'] as $rel => $links ) {			/*			 * If a list of relations was specified, and the link relation			 * is not in the list of allowed relations, don't process the link.			 */			if ( is_array( $embed ) && ! in_array( $rel, $embed, true ) ) {				continue;			} 			$embeds = array(); 			foreach ( $links as $item ) {				// Determine if the link is embeddable.				if ( empty( $item['embeddable'] ) ) {					// Ensure we keep the same order.					$embeds[] = array();					continue;				} 				if ( ! array_key_exists( $item['href'], $this->embed_cache ) ) {					// Run through our internal routing and serve.					$request = WP_REST_Request::from_url( $item['href'] );					if ( ! $request ) {						$embeds[] = array();						continue;					} 					// Embedded resources get passed context=embed.					if ( empty( $request['context'] ) ) {						$request['context'] = 'embed';					} 					if ( empty( $request['per_page'] ) ) {						$matched = $this->match_request_to_handler( $request );						if ( ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) {							$request['per_page'] = (int) $matched[1]['args']['per_page']['maximum'];						}					} 					$response = $this->dispatch( $request ); 					/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */					$response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request ); 					$this->embed_cache[ $item['href'] ] = $this->response_to_data( $response, false );				} 				$embeds[] = $this->embed_cache[ $item['href'] ];			} 			// Determine if any real links were found.			$has_links = count( array_filter( $embeds ) ); 			if ( $has_links ) {				$embedded[ $rel ] = $embeds;			}		} 		if ( ! empty( $embedded ) ) {			$data['_embedded'] = $embedded;		} 		return $data;	}

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.4.0
The $embed parameter can now contain a list of link relations to include.from the docblock
4.4.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/class-wp-rest-server.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.