wppaste
WordPress

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:957
Retrieves the route map.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
12–19

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

Plugin surface
1 hook

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

Called by
3

3 places 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 · 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.

WhenInstructionsCalls it makes
always12–13apply_filters()
always18–19namespace(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8012–1912
8.58012–1912
8.48012–19124 fewer instructions than PHP 8.3
8.38412–1912
8.28412–1912
8.18412–1912
7.48412–1912

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:

  1. apply_filters( rest_endpoints )filterline 974 (+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

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.

  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
Added $route_namespace parameter.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.