如何在 Wordpress 中编写个性化端点?
How do I write personalized endpoints in Wordpress?
我在 Wordpress 中有个性化的自定义字段以及 "user_meta" 等其他内容。我什至在 table 中向某些 post 类型添加了一些自定义字段。
现在我可以使用 wp-rest-api 调用或编写经典的 wordpress 变量。但它不能干扰我添加的私人区域。有不同类型的场景、先决条件和不同类型允许您读取/写入不同的字段,例如:"POST: https: //example.com/wp-json/wp/v2/posts? Title = .... & content = ... "
添加新文本的功能。
不过我的字体是 "fruits"。示例:"POST: https: //example.com/wp-json/wp/v2/fruits? Title = .... & content = ...."
如何编写自定义端点?
理解如何在 WordPress rest 中创建自定义端点的简单易行的方法 api 这个网站会有所帮助:https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
下面的例子将帮助您更深入地了解自定义端点的基础知识:
获取: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'
));
希望它能帮助您了解创建自定义端点的基本原理!
干杯
杰尼尔
我在 Wordpress 中有个性化的自定义字段以及 "user_meta" 等其他内容。我什至在 table 中向某些 post 类型添加了一些自定义字段。
现在我可以使用 wp-rest-api 调用或编写经典的 wordpress 变量。但它不能干扰我添加的私人区域。有不同类型的场景、先决条件和不同类型允许您读取/写入不同的字段,例如:"POST: https: //example.com/wp-json/wp/v2/posts? Title = .... & content = ... "
添加新文本的功能。
不过我的字体是 "fruits"。示例:"POST: https: //example.com/wp-json/wp/v2/fruits? Title = .... & content = ...."
如何编写自定义端点?
理解如何在 WordPress rest 中创建自定义端点的简单易行的方法 api 这个网站会有所帮助:https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
下面的例子将帮助您更深入地了解自定义端点的基础知识:
获取: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'
));
希望它能帮助您了解创建自定义端点的基本原理!
干杯
杰尼尔