By TTT


2019-04-10 14:30:12 8 Comments

I tried to include this code in my plug-in php files as well as in functions.php. (In the end I would like it to be in the plug-in's php file but I'm not yet sure if possible, that would probably be the topic of another question.)

It is a very basic method for now, I'm just trying to get a response with some content.

In both cases, I get a 404 response.

add_action( 'rest_api_init', function () {
  register_rest_route( plugin_dir_url(__DIR__).'my-project/api/v1/form', '/action', array(
    'methods' => 'GET, POST',
    'callback' => 'api_method',
  ) );
});

function api_method($data) {
    var_dump($data);
    return 'API method end.';
}

And I tried to access URLs (in brower or with AJAX)

I guess I'm missing something.

2 comments

@leymannx 2019-04-10 14:36:51

Maybe start with just GET. Your route looks weird as well. Try just:

register_rest_route('my-project/v1', '/action/', [
  'methods'  => WP_REST_Server::READABLE,
  'callback' => 'api_method',
]);

And your callback is not returning a valid response. Let your callback look more like this:

$data = [ 'foo' => 'bar' ];

$response = new WP_REST_Response($data, 200);

// Set headers.
$response->set_headers([ 'Cache-Control' => 'must-revalidate, no-cache, no-store, private' ]);

return $response;

Finally you must combine wp-json, the namespace my-project/v1 and your route action to the URL you now can check for what you get:

 https://my-domain.local/wp-json/my-project/v1/action

@TTT 2019-04-10 15:02:18

I just followed what you suggested, tried my-domain.local/my-project/v1 , my-domain.local/my-project/v1/get (with and without trailing slashes), still getting 404 response. At the moment, the code is in functions.php.

@leymannx 2019-04-10 15:12:34

@TTT – It must be my-domain.local/wp-json/my-project/v1/action as you've specified /action/ to be your route.

@TTT 2019-04-10 15:17:23

Thank you, this worked. Well since the answer is in the comment, I not sure if I should check the answer as solution right away. Also, I can make this return something else than JSON, right ? (I mean not like XML, like php processed HTML code.) ... Then the URL would still contain "wp-json". Weird ... or is there another type of API?

@Tom J Nowell 2019-04-10 15:20:00

@TTT you can store HTML in JSON as a string then JSON decode in the browser

@TTT 2019-04-10 16:01:03

I just found and tested this way here that looks cleaned than including HTML in JSON: gist.github.com/petenelson/6dc1a405a6e7627b4834

@leymannx 2019-04-10 16:27:59

@TTT – Uh nice! Thanks for sharing. Bookmarked.

@Tom J Nowell 2019-04-10 15:19:18

Here's your problem:

register_rest_route( plugin_dir_url(__DIR__).'my-project/api/v1/form', '/action', array(

Specifically the idea that this is possible:

http://my-domain.local/wp-content/plugins/my-project/api/v1/form

This is extremely unusual, and runs counter to what's in the docs, handbook, and tutorials.

REST API endpoints live at the REST API, which lives at the URL returned by rest_url(). They live at yoursite.com/wp-json. An endpoint is not a full URL path, or an independent API disconnected from the main API.

Instead, you need to define your endpoint names in terms of namespaces and endpoints, and visit the correct URL as described in the REST API's discovery mechanisms.

If we use this:

register_rest_route( plugin_dir_url(__DIR__).'my-project/api/v1/form', '/action', array(

Then we would expect this:

example.com/wp-json/wp-content/plugins/my-project/api/v1/form/action

That URL is quite long, and has a number of problems:

  • The first parameter is a namespace, not a URL
  • it's not possible to correctly separate out v1 of the API from v2 due to the way that that /form component has been put in the first parameter, not the second. The first parameter is a namespace, the second a route
  • /action is /action, it doesn't get swapped out for GET OR POST

There are also problems with the callback function:

function api_method($data) {
    var_dump($data);

An endpoint needs to return its data, it cannot output it directly as var_dump would, otherwise the returned data is invalid JSON.

Finally, the methods parameter is incorrect:

'methods' => 'GET, POST',

methods doesn't take a comma separated list, no docs suggest doing this either. Instead, use the predefined values provided by the REST API such as WP_REST_Server::READABLE or WP_REST_Server::ALLMETHODS, these are all mentioned in the handbook and the official documentation for register_rest_route.

A better route to register would be:

    register_rest_route( 'my-project/form/v1', '/action', array(

Giving us:

example.com/wp-json/my-project/form/v1/action

Notice how I removed the plugin URL and the redundant /api fragment ( it's obvious it's an API already )

@TTT 2019-04-10 15:25:42

Thank you for detailed explanation. I've already marked an answer that came quicker as solution however.

@Tom J Nowell 2019-04-10 17:21:00

You can change your mind about which answer is best, but eitherway think of the site as a wiki, there can be more than 1 good answer

@TTT 2019-04-10 17:42:32

One reason why I don't want to change answer is that I have conretely tested and applied the answer I selected as solution. Your answer brought more details and things I had thought in a wrong way somehow, but I haven't litteraly tested and checked it all. I also think that on most StackExchange sites, first good answers get selected, next one get votes. And though this has me scratching my head a bit, I see no reason to remove the solution rewards from the person who solved it first.

Related Questions

Sponsored Content

0 Answered Questions

Callback to custom field is not working in Wordpress REST API

2 Answered Questions

Sending post data over REST API, how to parse shortcodes in post_content?

2 Answered Questions

[SOLVED] How to feed a HTML5's EventSource with a REST API custom endpoint?

  • 2018-10-13 13:05:01
  • acidrums4
  • 288 View
  • 4 Score
  • 2 Answer
  • Tags:   rest-api

1 Answered Questions

How to get custom fields in a post when published

0 Answered Questions

0 Answered Questions

custom REST endpoint not passing body of POST request to callback

  • 2017-09-15 11:10:30
  • Jesterscup
  • 365 View
  • 0 Score
  • 0 Answer
  • Tags:   plugins rest-api

1 Answered Questions

[SOLVED] Update CPT meta data using REST API

2 Answered Questions

[SOLVED] How to use Wordpress rest API with Angularjs 4

  • 2017-04-19 07:08:44
  • Chirag S Modi
  • 7670 View
  • 0 Score
  • 2 Answer
  • Tags:   rest-api

2 Answered Questions

[SOLVED] How to call wp plugin REST functions without curl?

  • 2017-01-04 15:12:51
  • Jacksonkr
  • 1070 View
  • 0 Score
  • 2 Answer
  • Tags:   rest-api

1 Answered Questions

[SOLVED] Wordpress Api Get Drafts

  • 2016-09-22 18:30:42
  • Ronon
  • 1826 View
  • 2 Score
  • 1 Answer
  • Tags:   api rest-api draft

Sponsored Content