Php 为Wordpress API添加自定义端点
我想使用Wordpress的API方法和本文档为其构建一些自定义API端点 我发现他们解释了一切,但我不知道从哪里开始,我应该在哪里设置文件,或者在哪里设置文件 需要知道在哪里设置代码,在哪些文件中或将生成新的.php并包含它 如果有,也可以参考任何教程,在Wordpress中为该插件创建自定义端点 ByConsole电子商务订单交货时间管理Php 为Wordpress API添加自定义端点,php,wordpress,api,endpoint,Php,Wordpress,Api,Endpoint,我想使用Wordpress的API方法和本文档为其构建一些自定义API端点 我发现他们解释了一切,但我不知道从哪里开始,我应该在哪里设置文件,或者在哪里设置文件 需要知道在哪里设置代码,在哪些文件中或将生成新的.php并包含它 如果有,也可以参考任何教程,在Wordpress中为该插件创建自定义端点 ByConsole电子商务订单交货时间管理 提前感谢,关于复制,这里没有复制。我想问的是在哪里设置我的代码,而不是如何使用它。在WP中扩展REST API的简单方法 在下面的位置创建2个文件 1
提前感谢,关于复制,这里没有复制。我想问的是在哪里设置我的代码,而不是如何使用它。在WP中扩展REST API的简单方法 在下面的位置创建2个文件
1. wp-content/themes/yourTheme/API/wp-rest-api-base.php
2. wp-content/themes/yourTheme/API/wp-rest-api-func.php
然后在当前主题的functions.php
require get_parent_theme_file_path('API/wp-rest-api-base.php');
require get_parent_theme_file_path('API/wp-rest-api-func.php');
现在您有了2个自定义端点,可以通过
使用POST
方法:http://website.com/wp-json/api/v1/promotions
使用GET
方法:http://website.com/wp-json/api/v1/location_based_notify
apdefaultcontroller
construct要求在使用特定端点发出请求时调用方法名
注意:您的方法不应该返回JSON数据,因为它将由WP REST结构完成,您只需要返回数据数组,然后您的请求将返回JSON
响应
wp restapi base.php
<?php
class ApiBaseController extends WP_REST_Controller {
//The namespace and version for the REST SERVER
var $my_namespace = 'api/v';
var $my_version = '1';
public function register_routes() {
$namespace = $this->my_namespace.$this->my_version;
register_rest_route($namespace, '/promotions', array(
array(
'methods' => 'POST',
'callback' => array(new ApiDefaultController('cms_promotions'), 'init'),
)
)
);
register_rest_route($namespace, '/location_based_notify', array(
array(
'methods' => 'GET',
'callback' => array(new ApiDefaultController('location_based_notify'), 'init'),
)
)
);
}
// Register our REST Server
public function hook_rest_server() {
add_action('rest_api_init', array($this, 'register_routes'));
//add_action('rest_api_init', 'my_customize_rest_cors', 15);
}
public function my_customize_rest_cors() {
remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
remove_filter('rest_post_dispatch', 'rest_send_allow_header');
}
}
$ApiBaseController = new ApiBaseController();
$ApiBaseController->hook_rest_server();
<?php
class ApiDefaultController extends ApiBaseController
{
public $method;
public $response;
public function __construct($method)
{
$this->method = $method;
$this->response = array(
'Status' => false,
'StatusCode' => 0,
'StatusMessage' => 'Default'
);
}
private $status_codes = array(
'success' => true,
'failure' => 0,
'missing_param' => 150,
);
public function init(WP_REST_Request $request)
{
try {
if (!method_exists($this, $this->method)) {
throw new Exception('No method exists', 500);
}
$data = $this->{$this->method}($request);
$this->response['Status'] = $this->status_codes['success'];
$this->response['StatusCode'] = 1000;
$this->response['StatusMessage'] = 'success';
$this->response['Data'] = $data;
} catch (Exception $e) {
$this->response['Status'] = false;
$this->response['StatusCode'] = $e->getCode();
$this->response['StatusMessage'] = $e->getMessage();
}
return $this->response;
}
public function cms_promotions($request)
{
$data = array();
return $data;
}
public function location_based_notify($request)
{
$data = array();
return $data;
}
}
在WP中扩展RESTAPI的简单方法
在下面的位置创建2个文件
1. wp-content/themes/yourTheme/API/wp-rest-api-base.php
2. wp-content/themes/yourTheme/API/wp-rest-api-func.php
然后在当前主题的functions.php
require get_parent_theme_file_path('API/wp-rest-api-base.php');
require get_parent_theme_file_path('API/wp-rest-api-func.php');
现在您有了2个自定义端点,可以通过
使用POST
方法:http://website.com/wp-json/api/v1/promotions
使用GET
方法:http://website.com/wp-json/api/v1/location_based_notify
apdefaultcontroller
construct要求在使用特定端点发出请求时调用方法名
注意:您的方法不应该返回JSON数据,因为它将由WP REST结构完成,您只需要返回数据数组,然后您的请求将返回JSON
响应
wp restapi base.php
<?php
class ApiBaseController extends WP_REST_Controller {
//The namespace and version for the REST SERVER
var $my_namespace = 'api/v';
var $my_version = '1';
public function register_routes() {
$namespace = $this->my_namespace.$this->my_version;
register_rest_route($namespace, '/promotions', array(
array(
'methods' => 'POST',
'callback' => array(new ApiDefaultController('cms_promotions'), 'init'),
)
)
);
register_rest_route($namespace, '/location_based_notify', array(
array(
'methods' => 'GET',
'callback' => array(new ApiDefaultController('location_based_notify'), 'init'),
)
)
);
}
// Register our REST Server
public function hook_rest_server() {
add_action('rest_api_init', array($this, 'register_routes'));
//add_action('rest_api_init', 'my_customize_rest_cors', 15);
}
public function my_customize_rest_cors() {
remove_filter('rest_pre_serve_request', 'rest_send_cors_headers');
remove_filter('rest_post_dispatch', 'rest_send_allow_header');
}
}
$ApiBaseController = new ApiBaseController();
$ApiBaseController->hook_rest_server();
<?php
class ApiDefaultController extends ApiBaseController
{
public $method;
public $response;
public function __construct($method)
{
$this->method = $method;
$this->response = array(
'Status' => false,
'StatusCode' => 0,
'StatusMessage' => 'Default'
);
}
private $status_codes = array(
'success' => true,
'failure' => 0,
'missing_param' => 150,
);
public function init(WP_REST_Request $request)
{
try {
if (!method_exists($this, $this->method)) {
throw new Exception('No method exists', 500);
}
$data = $this->{$this->method}($request);
$this->response['Status'] = $this->status_codes['success'];
$this->response['StatusCode'] = 1000;
$this->response['StatusMessage'] = 'success';
$this->response['Data'] = $data;
} catch (Exception $e) {
$this->response['Status'] = false;
$this->response['StatusCode'] = $e->getCode();
$this->response['StatusMessage'] = $e->getMessage();
}
return $this->response;
}
public function cms_promotions($request)
{
$data = array();
return $data;
}
public function location_based_notify($request)
{
$data = array();
return $data;
}
}
您可以添加在functions.php
文件中提到的链接中提供的所有示例。看看会发生什么等等。理想情况下,您可以在以后构建自己的文件结构或插件。但是现在您可以在主题的functions.php
文件中工作。这里有更多示例:您可以添加functions.php
文件中提到的链接中提供的所有示例。看看会发生什么等等。理想情况下,您可以在以后构建自己的文件结构或插件。但是现在你可以在主题的functions.php
文件中工作。这里有更多的例子:谢谢你的回答将尝试它并反馈谢谢你的回答将尝试它并反馈