• How can i duplicate default wp rest api V2 endpoints? I would like to keep default endpoints and routs intact, but would like to use simplified responses for my application.

    My code for now:

    // 
    function register_custom_routes()
    {
        $controller = new MY_REST_Posts_Controller;
        $controller->register_routes();
    }
    
    add_action( 'rest_api_init', 'register_custom_routes', 1 );
    
    class MY_REST_Posts_Controller extends WP_REST_Controller {
     // this is a copy of default class WP_REST_Posts_Controller
    }

    Calling http://localhost/wp/wp-json/ list my namespace ( /myrest ), also
    http://localhost/wp/wp-json/myrest/gives me:

    {
      "namespace": "myrest",
      "routes": {
        "/myrest": {
          "namespace": "myrest",
          "methods": [
            "GET"
          ],
        ...
        ...
        "/myrest/(?P<id>[\\d]+)": {
          "namespace": "myrest",
          "methods": [
            "GET",
            "POST",
            "PUT",
            "PATCH",
            "DELETE"
          ],
         ...
         ...
    }

    but when i try to list posts with http://localhost/wp/wp-json/myrest/posts (like with default api route call) it does not wotk:

    {
      "code": "rest_no_route",
      "message": "No route was found matching the URL and request method",
      "data": {
        "status": 404
      }
    }

    Please help. I need simplified version of get posts response for android app but also want to keep default rest endpoints and routs as is.

    • This topic was modified 7 years, 4 months ago by ktwist.
Viewing 1 replies (of 1 total)
  • You probably figured this out already, but can try adding content type to the class.

    $controller = new MY_REST_Posts_Controller('post');

Viewing 1 replies (of 1 total)
  • The topic ‘Duplicate default endpoints’ is closed to new replies.