There are few problems:
-
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). -
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(). -
the
return new \WP_REST_Response($data, 200);
should bereturn 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);
}
}