WP_REST_Server::register_route() public WP 4.4.0
Registers a route to the server.
{} It's a method of the class: WP_REST_Server{}
No Hooks.
Return
Null. Nothing.
Usage
$WP_REST_Server = new WP_REST_Server(); $WP_REST_Server->register_route( $namespace, $route, $route_args, $override );
- $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. |
Code of WP_REST_Server::register_route() WP REST Server::register route WP 5.6
public function register_route( $namespace, $route, $route_args, $override = false ) {
if ( ! isset( $this->namespaces[ $namespace ] ) ) {
$this->namespaces[ $namespace ] = array();
$this->register_route(
$namespace,
'/' . $namespace,
array(
array(
'methods' => self::READABLE,
'callback' => array( $this, 'get_namespace_index' ),
'args' => array(
'namespace' => array(
'default' => $namespace,
),
'context' => array(
'default' => 'view',
),
),
),
)
);
}
// Associative to avoid double-registration.
$this->namespaces[ $namespace ][ $route ] = true;
$route_args['namespace'] = $namespace;
if ( $override || empty( $this->endpoints[ $route ] ) ) {
$this->endpoints[ $route ] = $route_args;
} else {
$this->endpoints[ $route ] = array_merge( $this->endpoints[ $route ], $route_args );
}
}