我对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上的WordPress 5.1.1 适用于Windows 7.3.3的XAMPP Apache / 2.4.38(Win64)OpenSSL / 1.1.1b PHP / 7.3.3 mysqlnd 5.0.12-dev-20150407
感谢所有人!
答案 0 :(得分: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();