# register_rest_route()

URL: https://chugunov.pro/api-wordpress/functions/register_rest_route/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 4.4.0.

## Сигнатура

```php
register_rest_route( string $route_namespace, string $route, array $args = array(), bool $override = false ): bool
```

## Описание

Примечание: не используйте до хука 'rest_api_init'.

## Параметры

- `$route_namespace` `string` — обязательный. Первый сегмент URL после префикса ядра. Должен быть уникальным для вашего пакета или плагина.
- `$route` `string` — обязательный. Базовый URL для добавляемого маршрута.
- `$args` `array` — необязательный, по умолчанию `array()`. Либо массив параметров для конечной точки, либо массив массивов для нескольких методов.
- `$override` `bool` — необязательный, по умолчанию `false`. Если маршрут уже существует, следует ли его переопределить? True переопределяет, false объединяет (при совпадающих ключах приоритет у более нового значения).

## Возвращаемое значение

`bool`

## Исходный код

Файл: `wp-includes/rest-api.php:34`

```php
function register_rest_route( $route_namespace, $route, $args = array(), $override = false ) {
	if ( empty( $route_namespace ) ) {
		/*
		 * Non-namespaced routes are not allowed, with the exception of the main
		 * and namespace indexes. If you really need to register a
		 * non-namespaced route, call `WP_REST_Server::register_route` directly.
		 */
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: string value of the namespace, 2: string value of the route. */
				__( 'Routes must be namespaced with plugin or theme name and version. Instead there seems to be an empty namespace \'%1$s\' for route \'%2$s\'.' ),
				'<code>' . $route_namespace . '</code>',
				'<code>' . $route . '</code>'
			),
			'4.4.0'
		);
		return false;
	} elseif ( empty( $route ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: string value of the namespace, 2: string value of the route. */
				__( 'Route must be specified. Instead within the namespace \'%1$s\', there seems to be an empty route \'%2$s\'.' ),
				'<code>' . $route_namespace . '</code>',
				'<code>' . $route . '</code>'
			),
			'4.4.0'
		);
		return false;
	}

	$clean_namespace = trim( $route_namespace, '/' );

	if ( $clean_namespace !== $route_namespace ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: string value of the namespace, 2: string value of the route. */
				__( 'Namespace must not start or end with a slash. Instead namespace \'%1$s\' for route \'%2$s\' seems to contain a slash.' ),
				'<code>' . $route_namespace . '</code>',
				'<code>' . $route . '</code>'
			),
			'5.4.2'
		);
	}

	if ( ! did_action( 'rest_api_init' ) ) {
		_doing_it_wrong(
			__FUNCTION__,
			sprintf(
				/* translators: 1: rest_api_init, 2: string value of the route, 3: string value of the namespace. */
				__( 'REST API routes must be registered on the %1$s action. Instead route \'%2$s\' with namespace \'%3$s\' was not registered on this action.' ),
				'<code>rest_api_init</code>',
				'<code>' . $route . '</code>',
				'<code>' . $route_namespace . '</code>'
			),
			'5.1.0'
		);
	}

	if ( isset( $args['args'] ) ) {
		$common_args = $args['args'];
		unset( $args['args'] );
	} else {
		$common_args = array();
	}

	if ( isset( $args['callback'] ) ) {
		// Upgrade a single set to multiple.
		$args = array( $args );
	}

	$defaults = array(
		'methods'  => 'GET',
		'callback' => null,
		'args'     => array(),
	);

	foreach ( $args as $key => &$arg_group ) {
		if ( ! is_numeric( $key ) ) {
			// Route option, skip here.
			continue;
		}

		$arg_group         = array_merge( $defaults, $arg_group );
		$arg_group['args'] = array_merge( $common_args, $arg_group['args'] );

		if ( ! isset( $arg_group['permission_callback'] ) ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: 1: The REST API route being registered, 2: The argument name, 3: The suggested function name. */
					__( 'The REST API route definition for %1$s is missing the required %2$s argument. For REST API routes that are intended to be public, use %3$s as the permission callback.' ),
					'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>',
					'<code>permission_callback</code>',
					'<code>__return_true</code>'
				),
				'5.5.0'
			);
		}

		foreach ( $arg_group['args'] as $arg ) {
			if ( ! is_array( $arg ) ) {
				_doing_it_wrong(
					__FUNCTION__,
					sprintf(
						/* translators: 1: $args, 2: The REST API route being registered. */
						__( 'REST API %1$s should be an array of arrays. Non-array value detected for %2$s.' ),
						'<code>$args</code>',
						'<code>' . $clean_namespace . '/' . trim( $route, '/' ) . '</code>'
					),
					'6.1.0'
				);
				break; // Leave the foreach loop once a non-array argument was found.
			}
		}
	}

	$full_route = '/' . $clean_namespace . '/' . trim( $route, '/' );
	rest_get_server()->register_route( $clean_namespace, $full_route, $args, $override );
	return true;
}
```

## История изменений

- 5.5.0 — Added a _doing_it_wrong() notice when the required permission_callback argument is not set.
- 5.1.0 — Added a _doing_it_wrong() notice when not called on or after the rest_api_init hook.
- 4.4.0 — Introduced.

## Связанные

Использует: [`rest_get_server`](https://chugunov.pro/api-wordpress/functions/rest_get_server/), [`did_action`](https://chugunov.pro/api-wordpress/functions/did_action/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`_doing_it_wrong`](https://chugunov.pro/api-wordpress/functions/_doing_it_wrong/).
Используется в: `WP_REST_Icons_Controller::register_routes`, `WP_HTTP_Polling_Sync_Server::register_routes`, `WP_REST_Abilities_V1_Categories_Controller::register_routes`, `WP_REST_Abilities_V1_Run_Controller::register_routes`, `WP_REST_Abilities_V1_List_Controller::register_routes`, `WP_REST_Font_Faces_Controller::register_routes`, `WP_REST_Font_Collections_Controller::register_routes`, `WP_REST_Template_Autosaves_Controller::register_routes`, `WP_REST_Template_Revisions_Controller::register_routes`, `WP_REST_Navigation_Fallback_Controller::register_routes`, `WP_REST_Global_Styles_Revisions_Controller::register_routes`, `WP_REST_Block_Patterns_Controller::register_routes`, `WP_REST_Block_Pattern_Categories_Controller::register_routes`, `WP_REST_Global_Styles_Controller::register_routes`, `WP_REST_URL_Details_Controller::register_routes`, `WP_REST_Menu_Locations_Controller::register_routes`, `WP_REST_Edit_Site_Export_Controller::register_routes`, `WP_REST_Widgets_Controller::register_routes`, `WP_REST_Sidebars_Controller::register_routes`, `WP_REST_Templates_Controller::register_routes`, `WP_REST_Pattern_Directory_Controller::register_routes`, `WP_REST_Widget_Types_Controller::register_routes`, `WP_REST_Site_Health_Controller::register_routes`, `WP_REST_Application_Passwords_Controller::register_routes`, `WP_REST_Block_Directory_Controller::register_routes`, `WP_REST_Plugins_Controller::register_routes`, `WP_REST_Block_Types_Controller::register_routes`, `WP_REST_Attachments_Controller::register_routes`, `WP_REST_Search_Controller::register_routes`, `WP_REST_Themes_Controller::register_routes`, `WP_REST_Autosaves_Controller::register_routes`, `WP_REST_Block_Renderer_Controller::register_routes`, `WP_REST_Users_Controller::register_routes`, `WP_REST_Revisions_Controller::register_routes`, `WP_REST_Post_Statuses_Controller::register_routes`, `WP_REST_Settings_Controller::register_routes`, `WP_REST_Terms_Controller::register_routes`, `WP_REST_Posts_Controller::register_routes`, `WP_REST_Taxonomies_Controller::register_routes`, `WP_REST_Post_Types_Controller::register_routes`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/register_rest_route/
