admin管理员组文章数量:1129156
Hi I've built a wordpress plugin using a class.
I'm sure all names (functies, urls, etc are correct.
I'm trying to access my endpoint url using http://localhost/inschrijfmodule/wp-json/endpoint/sportivity
with this code
add_action('rest_api_init', function(){
register_rest_route('endpoint', '/sportivity', [
'methods' => 'POST',
'callback' => [$this, 'sportivity_endpoint'],
'permission_callback' => function () {
return true;
}
]);
});
this is the error i'm receiving:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
Hi I've built a wordpress plugin using a class.
I'm sure all names (functies, urls, etc are correct.
I'm trying to access my endpoint url using http://localhost/inschrijfmodule/wp-json/endpoint/sportivity
with this code
add_action('rest_api_init', function(){
register_rest_route('endpoint', '/sportivity', [
'methods' => 'POST',
'callback' => [$this, 'sportivity_endpoint'],
'permission_callback' => function () {
return true;
}
]);
});
this is the error i'm receiving:
{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}
Share
Improve this question
asked Nov 27, 2023 at 10:00
JanJan
336 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 0This is why:
'methods' => 'POST',
Your REST API route is not being found because there is no route at that address that accepts a GET
request. There is an endpoint with that name that accepts POST
but it can only be accessed via a POST
request.
If you want to be able to visit it in a browser via the URL you need to do one of the following:
'methods' => 'GET',
'methods' => ['GET, 'POST' ],
Other things to note:
- In your endpoints callback, use the request object you were given to access the parameters and arguments, do not use
$_POST
or$_GET
- if you access them directly you bypass all the sanitisation and validation security features of the REST API, and have to write extra code to handle GET and POST that WP has already done for you
endpoint
is incredibly generic, and there's a good reason the WP endpoints use a version number, this should be changed!- Anonymous functions like that may look nice but they're awful to debug, don't show up nicely in stack traces, and are a nightmare if you ever have to un-hook/re-hook the callback.
- If you're just using a class as a place to put your functions then you're not doing object oriented programming, and there's no practical difference to functions in a namespace aside from all the extra typing. Save yourself from this turning into the god class anti-pattern
- a strong indicator of this is if your class has no member variables, is only constructed once, or contains your entire codebase
本文标签: plugin developmentEndpoint returning a 404
版权声明:本文标题:plugin development - Endpoint returning a 404 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736736252a1950271.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'permission_callback' => '__return_true'
– Tom J Nowell ♦ Commented Nov 27, 2023 at 10:14wp_die( 'running' );
inside the function beforeregister_rest_route
does it die withrunning
? You mentioned you used this inside a class but there is no class code in your question. It's also possible that because you used an anonymous function instead of a class method that$this
doesn't refer to what you expect it to, the solution might be to create apublic function register_rest_routes() : void { ....
class method and calladd_action('rest_api_init', [ $this, 'register_rest_routes' ]...
– Tom J Nowell ♦ Commented Nov 27, 2023 at 10:43wp_die()
came inside funtion beforeregister_rest_route
. I also tried your way like here pastebin.com/xiyjE8uD but this still does not solves the issue, besides that, it does not trigger the wp_die on the second spot – Jan Commented Nov 27, 2023 at 10:55register_rest_route
is being called, can you add any more information about when where and how your class is being used to construct the object? Is it happening in the same file, oninit
, infunctions.php
etc? – Tom J Nowell ♦ Commented Nov 27, 2023 at 14:19