禁用WP REST API的默认路由 [英] Disable default routes of WP REST API

查看:137
本文介绍了禁用WP REST API的默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要禁用 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 ?

解决方案

REST API Toolbox did the job for me.

We can handle many things via that plugin.

这篇关于禁用WP REST API的默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆