我只需通过以下方式处理WordPress的外部请求:http://localhost/vku.dev/wp-json/notifications.无需向整个URL添加其他部分。所以,我的代码是:
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\',
]);
});
代码看起来最简单,但不起作用-http://localhost/vku.dev/wp-json/notifications向我提供某个对象的转储,无法访问事件处理程序的主体。怎么了?