admin管理员组

文章数量:1317572

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
  • 2 I've never seen an endpoint that doesn't has a namespace but no endpoint name, it's extremely unusual, I'm surprised it's even allowed, can I ask why is your second parameter empty? – Tom J Nowell Commented Nov 2, 2020 at 11:28
  • 1 WordPress Codex doc (developer.wordpress/reference/functions/register_rest_route) say nothing about namespace and endpoint requirements. Anyway register_rest_route('vku/notifications', test'', [... works well. Thanks a lot – Valery Bulash Commented Nov 2, 2020 at 13:41
Add a comment  | 

1 Answer 1

Reset to default 1

Exact 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);
        },
    ]);
});

本文标签: phpSimple Wordpress endpoint route doesn39t work