rest_endpointsfilter-hookWP 4.4.0

Allows you to change the array of available REST API endpoints.

Usage

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(array)

An array of regular expressions for available endpoints, each mapped to an array of endpoint callbacks:

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

Examples

#1 Remove unnecessary endpoints

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

Changelog

Since 4.4.0 Introduced.

Where the hook is called

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

Where the hook is used in WordPress

Usage not found.