Wordpress WP API扩展 - 端点上的404

时间:2015-04-08 23:23:19

标签: wordpress-plugin wordpress

我正在尝试为我的插件创建一些自定义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

1 个答案:

答案 0 :(得分:0)

您需要确保已进入永久链接设置并保存以强制重写规则再次写入。

我在插件开发过程中使用的一个很好的工具,以确保实际应用重写规则是Rewrite Rules Inspector它列出了所有当前规则,包括新端点(如果已添加)。