WP_REST_Server::register_route() WordPress Method

The register_route() method is used to register a new route for the WordPress REST API. This method takes two arguments: the route and the callback. The route is the path that the API will use to access the resource. The callback is the function that will be called when the route is accessed. The callback function takes two arguments: the WP_REST_Request object and the WP_REST_Response object. The register_route() method can be used to create both simple and complex routes. For example, a simple route could be used to access a single post: register_route( 'posts/(?P\d+)', function( $request, $response ) { $post_id = $request['id']; $post = get_post( $post_id ); if ( ! $post ) { return new WP_Error( 'rest_post_invalid_id', __( 'Invalid post ID.' ), array( 'status' => 404 ) ); } $data = array( 'id' => $post_id, 'title' => $post->post_title, 'content' => $post->post_content, ); $response->set_data( $data ); return $response; } ); This route would be accessed at /wp-json/posts/, where is the post ID. The callback function would get the post with the given ID and return the title and content in the response. A more complex route could be used to access a post by its slug: register_route( 'posts/(?P[\w-]+)', function( $request, $response ) { $post_slug = $request['slug']; $post = get_page_by_path( $post_slug, OBJECT, 'post' ); if ( ! $post ) { return new WP_Error( 'rest_post_invalid_slug', __( 'Invalid post slug.' ), array( 'status' => 404 ) ); } $data = array( 'id' => $post->ID, 'title' => $post->post_title, 'content' => $post->post_content, ); $response->set_data( $data ); return $response; } ); This route would be accessed at /wp-json/posts/, where is the post slug. The callback function would get the post with the given slug and return the title and content in the response. Both of these routes are simple GET routes. The register_route() method can also be used to create more complex routes, such as POST routes.

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

Registers a route to the server.


Parameters

$namespace

(string)(Required)Namespace.

$route

(string)(Required)The REST route.

$route_args

(array)(Required)Route arguments.

$override

(bool)(Optional) Whether the route should be overridden if it already exists.

Default value: false


Top ↑

Source

File: wp-includes/rest-api/class-wp-rest-server.php

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


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.