admin管理员组文章数量:1277899
WP version is 5.5.3
I have 3 API routes set in a plugin that is used in an admin dashboard page. One route is meant to be used "publicly".
I have two very curious issues happening:
- My 3 admin-centric routes do not specify
permission_callback
. I should be getting notices but I do not when the docs and WP core functions say it will throw adoing_it_wrong
error. - My 4th public route does have
'permission_callback' => '__return_true'
set. I receive arest_not_logged_in
error code.
class My_Plugin
{
public function __construct()
{
add_action( 'rest_api_init', [ &$this, 'register_routes' ] );
}
public function register_routes(): void
{
register_rest_route('my-api-route', '/uri', [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_get_available_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[a-zA-Z0-9-]+)", [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_get_specific_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[0-9-]+)", [
'methods' => WP_REST_Server::EDITABLE,
'callback' => [&$this, 'api_update_specific_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[a-zA-Z0-9-]+)/load-more", [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_load_more_stuff'],
'permission_callback' => '__return_true',
]);
}
}
// header approach
$.ajax({
url: '/wp-json/my-api-route/uri/param/load-more',
method: 'GET',
headers: {
'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
},
data: {
'max_items': 5,
'offset': 5 * current_count,
},
})
// _wpnonce approach
$.ajax({
url: '/wp-json/my-api-route/uri/param/load-more',
method: 'GET',
data: {
'_wpnonce': '<?php echo wp_create_nonce('wp_rest'); ?>',
'max_items': 5,
'offset': 5 * current_count,
},
})
My only conclusion could be that, despite seeing "Version 5.5.3" in the bottom corner of WP Admin, I might not actually be on 5.5.3.
WP version is 5.5.3
I have 3 API routes set in a plugin that is used in an admin dashboard page. One route is meant to be used "publicly".
I have two very curious issues happening:
- My 3 admin-centric routes do not specify
permission_callback
. I should be getting notices but I do not when the docs and WP core functions say it will throw adoing_it_wrong
error. - My 4th public route does have
'permission_callback' => '__return_true'
set. I receive arest_not_logged_in
error code.
class My_Plugin
{
public function __construct()
{
add_action( 'rest_api_init', [ &$this, 'register_routes' ] );
}
public function register_routes(): void
{
register_rest_route('my-api-route', '/uri', [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_get_available_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[a-zA-Z0-9-]+)", [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_get_specific_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[0-9-]+)", [
'methods' => WP_REST_Server::EDITABLE,
'callback' => [&$this, 'api_update_specific_stuff'],
]);
register_rest_route('my-api-route', "/uri/(?P<param>[a-zA-Z0-9-]+)/load-more", [
'methods' => WP_REST_Server::READABLE,
'callback' => [&$this, 'api_load_more_stuff'],
'permission_callback' => '__return_true',
]);
}
}
// header approach
$.ajax({
url: '/wp-json/my-api-route/uri/param/load-more',
method: 'GET',
headers: {
'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
},
data: {
'max_items': 5,
'offset': 5 * current_count,
},
})
// _wpnonce approach
$.ajax({
url: '/wp-json/my-api-route/uri/param/load-more',
method: 'GET',
data: {
'_wpnonce': '<?php echo wp_create_nonce('wp_rest'); ?>',
'max_items': 5,
'offset': 5 * current_count,
},
})
My only conclusion could be that, despite seeing "Version 5.5.3" in the bottom corner of WP Admin, I might not actually be on 5.5.3.
Share Improve this question asked Dec 2, 2020 at 21:56 James WagonerJames Wagoner 111 silver badge2 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1Are you sure that a _doing_it_wrong
notice isn't being issued? You won't see the notice visibly output on the page because that would break the JSON response. But if you look in the headers of the response you should see a X-WP-DoingItWrong
header.
It should also appear if you use a plugin like this to record developer notices: https://wordpress/plugins/log-deprecated-notices/
本文标签: rest apipermissioncallback has no effect
版权声明:本文标题:rest api - permission_callback has no effect 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741232141a2362303.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_DEBUG
istrue
. 2. "I receive a rest_not_logged_in error code" - maybe that's being returned from yourapi_load_more_stuff()
function - check forrest_not_logged_in
in your callbacks. – Sally CJ Commented Dec 3, 2020 at 8:39