0

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..

1 Answer 1

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);
    }
}
2
  • notes for 2. and 3.: 2. -> yeah spotted that after posing, but changed to no effect. 3. It is defined within the actual code, I just shortened the function as it's not needed to see what it's doing :) but willl try point 1 :)
    – treyBake
    Commented Dec 12, 2018 at 10:27
  • It should do it, the print from your update indicates that the /vendor/v1/import accepts only method POST while you want to have GET (at least to test in browser). Commented Dec 12, 2018 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.