plugin development - REST Endpoint API 404

admin2025-06-05  3

I recently have been led to REST Endpoints within WordPress on another one of my posts.

However following the guides/provided solution results in a 404 of the expected endpoint API url.

My code is as follows:

wp-content/plugins/vendor-module/admin/admin-endpoint.php

<?php
    class Import_Csv
    {
        public function register_routes()
        {
            $version = 1;
            $namespace = sprintf('vendor/v%u', $version);
            $base = '/import';

            \register_rest_route(
                $namespace,
                $base,
                [
                    [
                        'methods' => \WP_REST_Server::CREATABLE,
                        'callback' => [$this, 'import_csv'],
                        'permission_callback' => [$this, 'get_import_permissions_check'],
                        'args' => []
                    ]
                ]
            );
        }

        public function get_import_permissions_check($req)
        {
            return true;
        }

        public function import_csv_file($req)
        {
            # the import process
            return new \WP_REST_Response($data, 200);
        }
    }

then in my plugins file I add the action:

wp-content/plugins/vendor-module/vendor-module.php

require_once 'admin/admin-endpoint.php';
add_action('rest_api_init', function()
{
    $import_csv = new \Import_Csv;
    $import_csv->register_routes();
});

I then go to this /index.php/wp-json/vendor/v1/import/ to test if it works but it returns a 404 error. What am I doing wrong in trying to create my endpoint?

**Edit: ** further testing

Going to the url /index.php/wp-json/vendor/v1/ shows this info:

{
  "namespace": "vendor/v1",
  "routes": {
    "/vendor/v1": {
      "namespace": "vendor/v1",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "vendor/v1"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "/index.php/wp-json/vendor/v1"
      }
    },
    "/vendor/v1/import": {
      "namespace": "vendor/v1",
      "methods": [
        "POST"
      ],
      "endpoints": [
        {
          "methods": [
            "POST"
          ],
          "args": []
        }
      ],
      "_links": {
        "self": "/index.php/wp-json/vendor/v1/import"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "/index.php/wp-json/"
      }
    ]
  }
}

So I can clearly see my link in that JSON string but for whatever reason, it's 404'ing when I actually go to it..

I recently have been led to REST Endpoints within WordPress on another one of my posts.

However following the guides/provided solution results in a 404 of the expected endpoint API url.

My code is as follows:

wp-content/plugins/vendor-module/admin/admin-endpoint.php

<?php
    class Import_Csv
    {
        public function register_routes()
        {
            $version = 1;
            $namespace = sprintf('vendor/v%u', $version);
            $base = '/import';

            \register_rest_route(
                $namespace,
                $base,
                [
                    [
                        'methods' => \WP_REST_Server::CREATABLE,
                        'callback' => [$this, 'import_csv'],
                        'permission_callback' => [$this, 'get_import_permissions_check'],
                        'args' => []
                    ]
                ]
            );
        }

        public function get_import_permissions_check($req)
        {
            return true;
        }

        public function import_csv_file($req)
        {
            # the import process
            return new \WP_REST_Response($data, 200);
        }
    }

then in my plugins file I add the action:

wp-content/plugins/vendor-module/vendor-module.php

require_once 'admin/admin-endpoint.php';
add_action('rest_api_init', function()
{
    $import_csv = new \Import_Csv;
    $import_csv->register_routes();
});

I then go to this http://site.local/index.php/wp-json/vendor/v1/import/ to test if it works but it returns a 404 error. What am I doing wrong in trying to create my endpoint?

**Edit: ** further testing

Going to the url http://site.local/index.php/wp-json/vendor/v1/ shows this info:

{
  "namespace": "vendor/v1",
  "routes": {
    "/vendor/v1": {
      "namespace": "vendor/v1",
      "methods": [
        "GET"
      ],
      "endpoints": [
        {
          "methods": [
            "GET"
          ],
          "args": {
            "namespace": {
              "required": false,
              "default": "vendor/v1"
            },
            "context": {
              "required": false,
              "default": "view"
            }
          }
        }
      ],
      "_links": {
        "self": "http://site.local/index.php/wp-json/vendor/v1"
      }
    },
    "/vendor/v1/import": {
      "namespace": "vendor/v1",
      "methods": [
        "POST"
      ],
      "endpoints": [
        {
          "methods": [
            "POST"
          ],
          "args": []
        }
      ],
      "_links": {
        "self": "http://site.local/index.php/wp-json/vendor/v1/import"
      }
    }
  },
  "_links": {
    "up": [
      {
        "href": "http://site.local/index.php/wp-json/"
      }
    ]
  }
}

So I can clearly see my link in that JSON string but for whatever reason, it's 404'ing when I actually go to it..

Share Improve this question edited Dec 12, 2018 at 10:24 treyBake asked Dec 12, 2018 at 10:06 treyBaketreyBake 12712 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There are few problems:

  1. you are using \WP_REST_Server::CREATABLE which is executed only when the request type is POST, if you are just opening the URL in the browser use \WP_REST_Server::READABLE instead (or \WP_REST_Server::ALLMETHODS to accept any request type).

  2. your callback is 'callback' => [$this, 'import_csv'], but it should be 'callback' => [$this, 'import_csv_file'], as the import_csv() method is not defined so i am guessing you meant import_csv_file().

  3. the return new \WP_REST_Response($data, 200); should be return new \WP_REST_Response($req, 200); as $data variable does not seem to be defined.

The complete code should look like this

class Import_Csv
{
    public function register_routes()
    {
        $version = 1;
        $namespace = sprintf('vendor/v%d', $version);
        $base = '/import/';

        \register_rest_route(
            $namespace,
            $base,
            [
                [
                    'methods' => \WP_REST_Server::READABLE,
                    'callback' => [$this, 'import_csv_file'],
                    'permission_callback' => [$this, 'get_import_permissions_check'],
                    'args' => []
                ]
            ]
        );
    }

    public function get_import_permissions_check($req)
    {
        return true;
    }

    public function import_csv_file($req)
    {
        # the import process
        return new \WP_REST_Response($req, 200);
    }
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749106230a316414.html

最新回复(0)