创建自定义端点Wordpress
问题描述:
我在创建自定义端点以扩展我的Wordpress应用程序时遇到问题。 http://localhost/wordpress/wp-json/创建自定义端点Wordpress
我链接测试里面的文件不同的端点:
建立WordPress的模块后,我可以通过给出的链接访问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;
}
但实际上我应该在哪里添加这些东西?此外,我研究了很多并看到了先进的端点控制器实践,有谁可以帮我一下吗?或者我需要创建自己的插件?
答
所有的代码转到主题的functions.php
文件或插件。注册一个REST路由后,它可以通过这个网址访问:
www.example.com/wp-json/myplugin/v1/foo