创建自定义终结点Wordpress [英] Creating custom endpoints Wordpress

查看:92
本文介绍了创建自定义终结点Wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建自定义终结点以扩展Wordpress应用程序时遇到麻烦.

I am having trouble on creating custom endpoints to extend my Wordpress application.

设置WordPress模块​​后,我可以通过给定的链接访问json数据: http://本地主机/wordpress/wp-json/

After setting up the WordPress module, I can access the json data through the link given : http://localhost/wordpress/wp-json/

我通过链接测试了文档内的不同端点: https://developer.wordpress.org/rest-api/reference/

I tested different endpoints inside the document with link: https://developer.wordpress.org/rest-api/reference/

现在我正在尝试创建自己的端点,但是经过大量研究,我只能找到类似的东西

And now I am trying to create my own endpoints, but after many researched I could only find something like

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;
}

但是实际上我应该在哪里添加这些内容?另外,我进行了很多研究,看到了先进的端点控制器实践,有人可以帮我吗?还是我需要创建自己的插件?

But indeed where should I add these things? Also, I researched a lots and see the advanced endpoint controller practice, may anyone give a hand on me? Or I need to create my own plugin?

推荐答案

所有代码都转到主题的functions.php文件或插件中.注册REST路由后,可以通过以下URL进行访问:

All the code goes to theme's functions.php file, or a plugin. After registered a REST route, it can be accessed via this URL:

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

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

这篇关于创建自定义终结点Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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