我在创建自定义端点以扩展我的WordPress应用程序时遇到了困难。
设置WordPress模块后,我可以通过给定的链接访问JSON数据:http://localhost/wordpress/wp-json/ 我使用链接https://developer.wordpress.org/rest-api/reference/测试了不同的端点。
现在我正在尝试创建自己的端点,但是经过多次研究,我只能找到类似以下内容:
并且然后。
设置WordPress模块后,我可以通过给定的链接访问JSON数据:http://localhost/wordpress/wp-json/ 我使用链接https://developer.wordpress.org/rest-api/reference/测试了不同的端点。
现在我正在尝试创建自己的端点,但是经过多次研究,我只能找到类似以下内容:
add_action( 'rest_api_init', 'myplugin_register_routes' );
并且然后。
function myplugin_register_routes() {
register_rest_route( 'myplugin/v1', 'foo', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'myplugin_serve_route',
));
}
function myplugin_serve_route( WP_REST_Request $request ) {
// Do something with the $request
// Return either a WP_REST_Response or WP_Error object
return $response;
}
但是,我应该在哪里添加这些内容呢?我进行了大量的研究,并查看了高级终端控制器实践,有人可以帮助我吗?还是我需要创建自己的插件?