WP_REST_Server::get_routes( string $route_namespace = '' ): array
- Since
- 4.4.0, 5.4.0
- Source
wp-includes/rest-api/class-wp-rest-server.php:956
Description
The route map is an associative array with path regexes as the keys. The value is an indexed array with the callback function/method as the first item, and a bitmask of HTTP methods as the second item (see the class constants).
Each route can be mapped to more than one callback by using an array of the indexed arrays. This allows mapping e.g. GET requests to one callback and POST requests to another.
Note that the path regexes (array keys) must have @ escaped, as this is used as the delimiter with preg_match()
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
$route_namespacestringoptional- Optionally, only return routes in the given namespace.Default:
''
Return value
array'/path/regex' => array( $callback, $bitmask )or'/path/regex' => array( array( $callback, $bitmask ), ...).
Performance profile
How much work a call to WP_REST_Server::get_routes() 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
- Scaling
- Scales with input
- Instructions
- 12–19
- Plugin surface
- 1 hook
- Called by
- 3
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 80.
Third-party callbacks on 'rest_endpoints' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Server::get_routes() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12–13 | apply_filters() |
| always | 18–19 | namespace(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 80 | 12–19 | 12 | |
| 8.5 | 80 | 12–19 | 12 | |
| 8.4 | 80 | 12–19 | 12 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 84 | 12–19 | 12 | |
| 8.2 | 84 | 12–19 | 12 | |
| 8.1 | 84 | 12–19 | 12 | |
| 7.4 | 84 | 12–19 | 12 |
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::get_routes() runs, in this order:
- apply_filters( rest_endpoints )filterline 973 (+17 into the body)
Filters the array of available REST API endpoints.
Uses · 3
- wp_list_filter()Filters a list of objects, based on a set of key => value arguments.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_args()Merges user defined arguments into defaults array.
Used by · 3
- WP_REST_Server::get_index()Retrieves the site index.
- WP_REST_Server::get_namespace_index()Retrieves the index for a namespace.
- WP_REST_Server::match_request_to_handler()Matches a request object to its handler.
Source code
public function get_routes( $route_namespace = '' ) { $endpoints = $this->endpoints; if ( $route_namespace ) { $endpoints = wp_list_filter( $endpoints, array( 'namespace' => $route_namespace ) ); } /** * Filters the array of available REST API endpoints. * * @since 4.4.0 * * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped * to an array of callbacks for the endpoint. These take the format * `'/path/regex' => array( $callback, $bitmask )` or * `'/path/regex' => array( array( $callback, $bitmask ). */ $endpoints = apply_filters( 'rest_endpoints', $endpoints ); // Normalize the endpoints. $defaults = array( 'methods' => '', 'accept_json' => false, 'accept_raw' => false, 'show_in_index' => true, 'args' => array(), ); foreach ( $endpoints as $route => &$handlers ) { if ( isset( $handlers['callback'] ) ) { // Single endpoint, add one deeper. $handlers = array( $handlers ); } if ( ! isset( $this->route_options[ $route ] ) ) { $this->route_options[ $route ] = array(); } foreach ( $handlers as $key => &$handler ) { if ( ! is_numeric( $key ) ) { // Route option, move it to the options. $this->route_options[ $route ][ $key ] = $handler; unset( $handlers[ $key ] ); continue; } $handler = wp_parse_args( $handler, $defaults ); // Allow comma-separated HTTP methods. if ( is_string( $handler['methods'] ) ) { $methods = explode( ',', $handler['methods'] ); } elseif ( is_array( $handler['methods'] ) ) { $methods = $handler['methods']; } else { $methods = array(); } $handler['methods'] = array(); foreach ( $methods as $method ) { $method = strtoupper( trim( $method ) ); $handler['methods'][ $method ] = true; } } } return $endpoints; }Changelog
Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$route_namespace parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.