我正在尝试为我的插件创建一些自定义JSON端点。我安装了WP API插件,下面是我的插件php的全部内容。
<?php
/**
* Plugin Name: Quiz Wordpress Plugin
* Plugin URI: http://www.domain.com
* Description: This plugin handles quiz functionality.
* Version: 1.0.0
* Author: Ray Hwang
* Author URI: http://www.domain.com
* License: private
*/
function quiz_api_init() {
global $quiz_api;
$quiz_api = new QUIZ_API();
add_filter( 'json_endpoints', array( $quiz_api, 'register_routes' ) );
}
add_action( 'wp_json_server_before_serve', 'quiz_api_init' );
class QUIZ_API {
public function register_routes( $routes ) {
$routes['/api'] = array(
array( array( $this, 'get_quiz'), WP_JSON_Server::READABLE ),
array( array( $this, 'new_quiz'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON ),
);
return $routes;
}
public function get_quiz($_headers, $data = ''){
return array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
}
public function new_quiz($_headers, $data = ''){
return array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
}
}
当我激活插件时没有错误。当我请求端点时。我得到
Not Found
The requested URL /api was not found on this server.
Apache/2.4.7 (Ubuntu) Server at ec2-----.compute-1.amazonaws.com Port 80
这与URL重写或永久链接有关吗?
我的永久链接设置设置为默认http://ec2--------.compute-1.amazonaws.com/?p=123
您需要确保已进入永久链接设置并保存,以强制重新编写重写规则。
我在插件开发过程中使用的一个很好的工具是"重写规则检查器",它会列出所有当前规则,包括添加的新端点。