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