WordPress自定义端点 - rest_invalid_handler

问题描述 投票:0回答:1

我是PHP和WordPress的新手,但是我开始编写应该能够公开自定义端点的第一个插件。我设法注册并激活插件和路由,但是当我尝试访问此端点时,我收到以下错误:

// http://localhost/myWp/wp-json/myCustomPlugin/v2/lastpost

{
  "code": "rest_invalid_handler",
  "message": "The handler for the route is invalid",
  "data": {
    "status": 500
  }
}

如果我访问:http://localhost/myWp/wp-json/myCustomPlugin/v2/,看起来我的端点已成功注册:

// 20190420173834
// http://localhost/myWp/wp-json/myCustomPlugin/v2/

{
  "namespace": "myCustomPlugin/v2",
  "routes": {
    "/myCustomPlugin/v2": {
      "namespace": "myCustomPlugin/v2",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "myCustomPlugin/v2"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "http://localhost/myWp/wp-json/myCustomPlugin/v2"
      }
    },
    "/myCustomPlugin/v2/lastpost": {
      "namespace": "myCustomPlugin/v2",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": [

          ]
        }
      ],
      "_links": {
        "self": "http://localhost/myWp/wp-json/myCustomPlugin/v2/lastpost"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "http://localhost/myWp/wp-json/"
      }
    ]
  }
}

最后我提供了我的插件的代码:

<?php

/**
 * @package myCustomPlugin
 *
 * /
 *
 /*
  Plugin Name: My Custom Plugin
  Plugin URI: https://mypage.net
  Description: my custom plugin
  Version: 1.0.0
  Author: My Name
  Author URI: https://mypage.net
  License: Proprierty
  Text Domain: myCustomPlugin

  */
  /*it means someone outside wp is accessing the file, in this case kill it.
  */

  if(!defined('ABSPATH'))
  {
      die ('You can not access this file!');
  }

  class MyCustomPlugin
  {

      //methods
      function activate(){
          flush_rewrite_rules();
      }

      function register(){
          add_action('rest_api_init', function () {
            register_rest_route('myCustomPlugin/v2', 'lastpost',array(
                          'methods'  => 'GET',
                          'callback' => 'get_last_post'
                ));
          });
      }

      function get_last_post($request) {
        return "it works";
      }

      function deactivate(){
           echo 'The MyCustomPlugin was deactivated';
           flush_rewrite_rules();
      }
  }

  register_activation_hook( __FILE__, array($myCustomPlugin,'activate'));
  register_activation_hook( __FILE__, array($myCustomPlugin,'deactivate'));

  $myCustomPlugin = new MyCustomPlugin();
  $myCustomPlugin->register();

我正在使用:Windows 10 XAMPP for Windows 7.3.3上的WordPress 5.1.1 Apache / 2.4.38(Win64)OpenSSL / 1.1.1b PHP / 7.3.3 mysqlnd 5.0.12-dev - 20150407

感谢All提前!

php wordpress rest plugins
1个回答
1
投票

试试这个

<?php

/**
 * @package myCustomPlugin
 *
 * /
 *
  /*
  Plugin Name: My Custom Plugin
  Plugin URI: https://mypage.net
  Description: my custom plugin
  Version: 1.0.0
  Author: My Name
  Author URI: https://mypage.net
  License: Proprierty
  Text Domain: myCustomPlugin

 */
/* it means someone outside wp is accessing the file, in this case kill it.
 */

if (!defined('ABSPATH')) {
die('You can not access this file!');
}

class MyCustomPlugin {

function __construct() {
    add_action('rest_api_init', array($this, 'register'));
}

//methods
function activate() {
    flush_rewrite_rules();
}

function register() {

    register_rest_route('myCustomPlugin/v2', '/lastpost', array(
        'methods' => 'GET',
        'callback' => array($this,'get_last_post')
    ));
}

function get_last_post() {
    return new WP_REST_Response("ok", 200);
}

function deactivate() {
    echo 'The MyCustomPlugin was deactivated';
    flush_rewrite_rules();
}

}

register_activation_hook(__FILE__, array($myCustomPlugin, 'activate'));
register_activation_hook(__FILE__, array($myCustomPlugin, 'deactivate'));

$myCustomPlugin = new MyCustomPlugin();
© www.soinside.com 2019 - 2024. All rights reserved.