Simple WordPress endpoint route doesn’t work

Question

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?

0
Valery Bulash 3 months 0 Answers 18 views 0

Leave an answer