Disable default routes of WP REST API Disable default routes of WP REST API wordpress wordpress

Disable default routes of WP REST API


This question has already accepted answer. But if anybody find this useful.We can easily remove default routes. Add following code in your theme's (child theme's if any) functions.php or in any custom plugin

add_filter('rest_endpoints', function( $endpoints ) {    foreach( $endpoints as $route => $endpoint ){        if( 0 === stripos( $route, '/wp/' ) ){            unset( $endpoints[ $route ] );        }    }    return $endpoints;});


As per the other question, this is the only "clean" way to do it currently. The cleanest way to approach things in Wordpress is by using filters and/or actions - this allows you to interact with core without making changes in core.

By tapping into filters/actions you are also giving other plugins the chance to operate on the filter/action arguments before/after your hook.

If you take a look at class-wp-rest-server.php you can easily look at all available filters and actions related to rest.

You will notice this one in particular:

    /**     * Filters the array of available endpoints.     *     * @since 4.4.0     *     * @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 ).     */    $endpoints = apply_filters( 'rest_endpoints', $this->endpoints );

From my research, this is the very last spot to modify (remove, change or add) endpoints, and is the exact purpose of the filter.

As a sidenote, you don't need to do it "one by one" - you could just do $endpoints = [] to start fresh.


REST API Toolbox did the job for me.

We can handle many things via that plugin.

enter image description here