I need to just handle outer request to my WordPress by: http://localhost/vku.dev/wp-json/notifications. No need to add additional parts to whole URL. So, my code is:
function testREST(WP_REST_Request $request) {
error_log('Route "notifications" works');
return 'Route "notifications" works';
//return new WP_REST_Response(true, 200);
}
add_action('rest_api_init', function () {
/*
// Inline function also not working
register_rest_route('notifications', '', [
'methods' => 'GET',
'callback' => function (WP_REST_Request $request) {
error_log('Route "notifications" works');
return 'Route "notifications" works';
//return new WP_REST_Response(true, 200);
},
]);
*/
register_rest_route('notifications', '', [
'methods' => 'GET',
'callback' => 'testREST',
]);
});
Code looks simplest, but not working - http://localhost/vku.dev/wp-json/notifications provide me with the dump of some object, body of event handler not reachable. What's wrong?
I need to just handle outer request to my WordPress by: http://localhost/vku.dev/wp-json/notifications. No need to add additional parts to whole URL. So, my code is:
function testREST(WP_REST_Request $request) {
error_log('Route "notifications" works');
return 'Route "notifications" works';
//return new WP_REST_Response(true, 200);
}
add_action('rest_api_init', function () {
/*
// Inline function also not working
register_rest_route('notifications', '', [
'methods' => 'GET',
'callback' => function (WP_REST_Request $request) {
error_log('Route "notifications" works');
return 'Route "notifications" works';
//return new WP_REST_Response(true, 200);
},
]);
*/
register_rest_route('notifications', '', [
'methods' => 'GET',
'callback' => 'testREST',
]);
});
Code looks simplest, but not working - http://localhost/vku.dev/wp-json/notifications provide me with the dump of some object, body of event handler not reachable. What's wrong?
Share Improve this question asked Nov 2, 2020 at 10:33 Valery BulashValery Bulash 1112 bronze badges 2 |1 Answer
Reset to default 1Exact namespace prefix and non-empty endpoint name helped me:
add_action('rest_api_init', function () {
register_rest_route('vku/notifications', 'test', [
'methods' => 'GET',
'callback' => function (WP_REST_Request $request) {
error_log('Route "notifications" works');
return new WP_REST_Response(true, 200);
},
]);
});
register_rest_route('vku/notifications', test'', [...
works well. Thanks a lot – Valery Bulash Commented Nov 2, 2020 at 13:41