2017-08-05 82 views
0

我在創建自定義端點以擴展我的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; 
} 

但實際上我應該在哪裏添加這些東西?此外,我研究了很多並看到了先進的端點控制器實踐,有誰可以幫我一下嗎?或者我需要創建自己的插件?

回答

1

所有的代碼轉到主題的functions.php文件或插件。註冊一個REST路由後,它可以通過這個網址訪問:

www.example.com/wp-json/myplugin/v1/foo