WP_REST_Server::register_route( string $route_namespace, string $route, array $route_args, bool $override = false )

In this article

Registers a route to the server.

Parameters

$route_namespacestringrequired
Namespace.
$routestringrequired
The REST route.
$route_argsarrayrequired
Route arguments.
$overridebooloptional
Whether the route should be overridden if it already exists.

Default:false

Source

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 );
	}
}

Changelog

VersionDescription
4.4.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.