WP_REST_Server::register_route()publicWP 4.4.0

Registers a route to the server.

Method of the class: WP_REST_Server{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_REST_Server = new WP_REST_Server();
$WP_REST_Server->register_route( $route_namespace, $route, $route_args, $override );
$route_namespace(string) (required)
Namespace.
$route(string) (required)
The REST route.
$route_args(array) (required)
Route arguments.
$override(true|false)
Whether the route should be overridden if it already exists.
Default: false

Changelog

Since 4.4.0 Introduced.

WP_REST_Server::register_route() code WP 6.5.2

public function register_route( $route_namespace, $route, $route_args, $override = false ) {
	if ( ! isset( $this->namespaces[ $route_namespace ] ) ) {
		$this->namespaces[ $route_namespace ] = array();

		$this->register_route(
			$route_namespace,
			'/' . $route_namespace,
			array(
				array(
					'methods'  => self::READABLE,
					'callback' => array( $this, 'get_namespace_index' ),
					'args'     => array(
						'namespace' => array(
							'default' => $route_namespace,
						),
						'context'   => array(
							'default' => 'view',
						),
					),
				),
			)
		);
	}

	// Associative to avoid double-registration.
	$this->namespaces[ $route_namespace ][ $route ] = true;

	$route_args['namespace'] = $route_namespace;

	if ( $override || empty( $this->endpoints[ $route ] ) ) {
		$this->endpoints[ $route ] = $route_args;
	} else {
		$this->endpoints[ $route ] = array_merge( $this->endpoints[ $route ], $route_args );
	}
}