WordPress WP API Extending – 404 on Endpoints

I’m trying to create a few custom JSON endpoints for my plugin. I installed the WP API plugin and here’s the entire contents of my plugin 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);
    }
}

There are no errors when I activate the plugin. When I request the endpoint. I get

Read More
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

Does it have something to do with URL rewrite or permalinks?

My permalink settings is set to default http://ec2--------.compute-1.amazonaws.com/?p=123

Related posts

Leave a Reply

1 comment

  1. You will need to ensure you’ve gone into permalink settings and saved to force the rewrite rules to be written again.

    Also a great tool I use during my plugin development to ensure that the rewrite rules are actually applied is Rewrite Rules Inspector it’ll list all your current rules including your new endpoints if they have been added.