为 Wordpress API 添加自定义端点

Ahm*_*emy 1 php api wordpress endpoint

我想使用其 API 方法为 Wordpress 构建一些自定义 API 端点,并参考本文档

https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

我发现他们解释了一切,但我不知道从哪里开始我应该在哪里设置我的文件或在哪里设置它们。

需要知道在哪些文件中设置代码或将生成新的 .php 并包含它???

另外,如果有的话可以参考任何教程来在 WordPress 中为此插件构建自定义端点

ByConsole Woocommerce 订单交付时间管理

预先感谢,关于重复,这里不重复,我询问在哪里设置我的代码而不是如何使用它。

Nom*_*man 5

在 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
Run Code Online (Sandbox Code Playgroud)

然后将这两个文件包含在当前主题的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');
Run Code Online (Sandbox Code Playgroud)

现在您有 2 个自定义端点,可以通过以下方式访问

POST方法:http://website.com/wp-json/api/v1/promotions

GET方法:http://website.com/wp-json/api/v1/location_based_notify

ApiDefaultController构造需要在向特定端点发出请求时调用的方法名称。

注意:您的方法不应返回 JSON 数据,因为它将由 WP REST 结构完成,您需要的只是返回数据数组,然后您的请求将返回JSON响应。

wp-rest-api-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();
Run Code Online (Sandbox Code Playgroud)

wp-rest-api-func.php

<?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;
    }
}
Run Code Online (Sandbox Code Playgroud)