wordpressrestrouteswp-api

Disable default routes of WP REST API


I need to disable default routes of WP REST API and add custom routes.

I found this question which helps me to find following answer.

remove_action('rest_api_init', 'create_initial_rest_routes', 99);

However this will also remove any custom content type routes. So instead you may choose to use:

add_filter('rest_endpoints', function($endpoints) {
    if ( isset( $endpoints['/wp/v2/users'] ) ) {
        unset( $endpoints['/wp/v2/users'] );
    }
    // etc
});

But from that way I need to know all the default routes and remove one by one which is not a cleanest way to do.

I would like to know whether there is any cleaner way to achieve that ?

UPDATE 1 :

As per the Chris's suggestion I would add more details to question.

Currently I am using rest_api_init filter to add my custom routes by using register_rest_route method as per the below code which I found on this article.

add_action( 'rest_api_init', function () {
  register_rest_route( 'myplugin/v1', '/sample/', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
  ) );
} );

function my_awesome_func( $data ) {
  return 'somthing';
}

The custom route works well, but unfortunately I can't disable the default routes such as /wp/v2/posts.

My question :

How to unset/disable the default routes while using the rest_api_init filter to register new custom routes ?


Solution

  • REST API Toolbox did the job for me.

    We can handle many things via that plugin.

    enter image description here