php - WordPress 自定义端点 - rest_invalid_handler
问题描述
我对 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();
我正在使用: WordPress 5.1.1 on Windows 10 XAMPP for Windows 7.3.3 Apache/2.4.38 (Win64) OpenSSL/1.1.1b PHP/7.3.3 mysqlnd 5.0.12-dev - 20150407
提前感谢所有人!
解决方案
尝试这个
<?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();
推荐阅读
- haskell - 用 let 和 show 在 haskell 中列出理解,它有什么用?
- css - 如何使用反应在 CSS 内容上获取当前 URL?
- r - 使用总和约束生成排列
- node.js - Dockerizing a React App:应用程序在容器内启动,但无法从暴露的端口访问
- embedded-linux - Buildroot linux内核干净构建
- javascript - 打开file.txt并在输入文本框节点js中显示内容
- c++ - 如何在另一个类中定义模板类变量?
- javascript - Angular / TypeScript,当使用多个组件时,第一个组件影响第二个组件
- scala - 如何使用 mapPartition 等在执行程序内部进行涉及 rdd 的操作?
- r - 如何使用 lubridate 从 R 中大型数据集中的单列中提取开始和结束日期?