rest_endpointsхук-фильтрWP 4.4.0

Позволяет изменить массив доступных конечных точек REST API.

Использование

add_filter( 'rest_endpoints', 'wp_kama_rest_endpoints_filter' );

/**
 * Function for `rest_endpoints` filter-hook.
 * 
 * @param array $endpoints The available endpoints. An array of matching regex patterns, each mapped to an array of callbacks for the endpoint. These take the format `'/path/regex' => array( $callback, $bitmask )` or `'/path/regex' => array( array( $callback, $bitmask ).
 *
 * @return array
 */
function wp_kama_rest_endpoints_filter( $endpoints ){

	// filter...
	return $endpoints;
}
$endpoints(массив)

Массив regex доступных конечных точек, каждый из которых сопоставлен с массивом коллбэков конечной точки:

'/path/regex' => [ $callback, $bitmask ]
// или
'/path/regex' => [ [ $callback, $bitmask ], ... ]

Примеры

0

#1 Удаление ненужных эндпоинтов

add_filter( 'rest_endpoints', 'remove_needless_endpoints' );
function remove_needless_endpoints( $endpoints ) {

	if ( ! is_front_page() ) {
		return $endpoints;
	}

	$remove = [
		'/',
		'wp/v2/posts',
		'wp/v2/pages',
		'wp/v2/media',
		'wp/v2/menu-items',
		'wp/v2/blocks',
		'wp/v2/templates',
		'wp/v2/template-parts',
		'wp/v2/navigation',
		'wp/v2/types',
		'wp/v2/statuses',
		'wp/v2/taxonomies',
		'wp/v2/categories',
		'wp/v2/tags',
		'wp/v2/menus',
		'wp/v2/comments',
		//'wp/v2/options',
		//'wp/v2/users',
		//'wp/v2/taxonomies',
		//'wp/v2/post-types',
		//'wp/v2/menus',
		//'wp/v2/revisions',
		//'wp/v2/post-formats',
		//'wp/v2/wp-embed',
		//'wp/v2/wp-comments',
	];

	$filter_callback = static function ( $route ) use ( $remove ) {
		foreach ( $remove as $needle ) {
			if ( ( '/' === $needle && '/' === $route ) || str_contains( $route, $needle ) ) {
				return false;
			}
		}
		return true;
	};

	return array_filter( $endpoints, $filter_callback, ARRAY_FILTER_USE_KEY );
}

Список изменений

С версии 4.4.0 Введена.

Где вызывается хук

WP_REST_Server::get_routes()
rest_endpoints
wp-includes/rest-api/class-wp-rest-server.php 973
$endpoints = apply_filters( 'rest_endpoints', $endpoints );

Где используется хук в WordPress

Использование не найдено.