如何在Wordpress中编写个性化的终结点?

时间:2019-07-31 12:19:35

标签: wordpress rest wordpress-rest-api

我在Wordpress中具有个性化的自定义字段以及诸如“ user_meta”之类的其他内容。我什至在表格中向某些帖子类型添加了一些自定义字段。 现在,我可以使用wp-rest-api调用或编写经典的wordpress变量。但这不会干扰我添加的私有区域。有不同类型的方案,先决条件和不同类型,可让您读取/写入不同的字段,例如:"POST: https: //example.com/wp-json/wp/v2/posts? Title = .... & content = ... "用于添加新文本的功能。

虽然我的字体是“水果”。示例:"POST: https: //example.com/wp-json/wp/v2/fruits? Title = .... & content = ...."如何编写自定义端点?

1 个答案:

答案 0 :(得分:0)

了解如何在此网站的WordPress rest api中创建自定义终结点的简单方法将非常有用:https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

下面的示例将帮助您更多地了解自定义端点的基本知识:

GET:https://example.com/wp-json/wp/v2/hello/world

定义操作:

add_action( 'rest_api_init', function(){
    register_rest_route( 'wp/v2/hello', 'world', array(
        'methods' => 'GET',
        'callback' => 'rest_hello_world'
    ));
});

回叫功能定义:

function rest_hello_world(){
   return "Hello World";
}

要创建 POST 方法调用,只需将方法参数更改为POST:

register_rest_route( 'wp/v2/hello', 'world', array(
    'methods' => 'POST',
    'callback' => 'rest_hello_world'
));

希望它可以帮助您了解创建自定义端点的基本原理!


干杯
詹妮(Jenil)