WP_REST_Templates_Controller::prepare_links( integer $id ): array
- Since
- 5.8.0
- Source
wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php:943
Compatibility
- WordPress
- since 5.8.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
$idinteger- ID.
Return value
array- Links for the given post.
Performance profile
How much work a call to WP_REST_Templates_Controller::prepare_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
- Heavy
- Scaling
- Constant
- Instructions
- 40–93
- Plugin surface
- None
- Called by
- 1
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 94.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below WP_REST_Templates_Controller::prepare_links() - querycontent query
get_post()one call below WP_REST_Templates_Controller::prepare_links()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Templates_Controller::prepare_links() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!post_type_supports() | 40 | rest_url(), rest_get_route_for_post_type_items(), rest_url(), rest_url(), post_type_supports() |
post_type_supports() | 49–51 | rest_url(), rest_get_route_for_post_type_items(), rest_url(), rest_url(), post_type_supports(), get_block_template() |
post_type_supports() && $template instanceof && !empty($template) && !$revisions_count | 81–82 | rest_url(), rest_get_route_for_post_type_items(), rest_url(), rest_url(), post_type_supports(), get_block_template(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), rest_url() |
post_type_supports() && $template instanceof && !empty($template) && $revisions_count | 92–93 | rest_url(), rest_get_route_for_post_type_items(), rest_url(), rest_url(), post_type_supports(), get_block_template(), wp_get_latest_revision_id_and_total_count(), is_wp_error(), rest_url(), rest_url() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 94 | 40–93 | 5 | |
| 8.5 | 94 | 40–93 | 5 | |
| 8.4 | 94 | 40–93 | 5 | |
| 8.3 | 94 | 40–93 | 5 | |
| 8.2 | 94 | 40–93 | 5 | |
| 8.1 | 94 | 40–93 | 5 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 95 | 40–94 | 5 |
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.
Uses · 6
- rest_url()Retrieves the URL to a REST endpoint.
- rest_get_route_for_post_type_items()Gets the REST API route for a post type.
- post_type_supports()Checks a post type's support for a given feature.
- get_block_template()Retrieves a single unified template object using its id.
- wp_get_latest_revision_id_and_total_count()Returns the latest revision ID and count of revisions for a post.
- is_wp_error()Checks whether the given variable is a WordPress Error.
Used by · 1
- WP_REST_Templates_Controller::prepare_item_for_response()Prepare a single template output for response
Source code
protected function prepare_links( $id ) { $links = array( 'self' => array( 'href' => rest_url( sprintf( '/%s/%s/%s', $this->namespace, $this->rest_base, $id ) ), ), 'collection' => array( 'href' => rest_url( rest_get_route_for_post_type_items( $this->post_type ) ), ), 'about' => array( 'href' => rest_url( 'wp/v2/types/' . $this->post_type ), ), ); if ( post_type_supports( $this->post_type, 'revisions' ) ) { $template = get_block_template( $id, $this->post_type ); if ( $template instanceof WP_Block_Template && ! empty( $template->wp_id ) ) { $revisions = wp_get_latest_revision_id_and_total_count( $template->wp_id ); $revisions_count = ! is_wp_error( $revisions ) ? $revisions['count'] : 0; $revisions_base = sprintf( '/%s/%s/%s/revisions', $this->namespace, $this->rest_base, $id ); $links['version-history'] = array( 'href' => rest_url( $revisions_base ), 'count' => $revisions_count, ); if ( $revisions_count > 0 ) { $links['predecessor-version'] = array( 'href' => rest_url( $revisions_base . '/' . $revisions['latest_id'] ), 'id' => $revisions['latest_id'], ); } } } return $links; }Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.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.