简单的WordPress端点路由不起作用

时间:2020-11-02 作者:Valery Bulash

我只需通过以下方式处理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向我提供某个对象的转储,无法访问事件处理程序的主体。怎么了?

1 个回复
SO网友:Valery Bulash

准确的命名空间前缀和非空端点名称帮助了我:

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

相关推荐

如何通过unctions.php有条件地加载soronomy-{soronomy}.php模板php文件

我有一个电影评论网站,我想有条件地加载演员和导演的分类,只有当我的主题选项处于活动状态时。目前在根主题文件夹中,我有以下两个文件taxonomy-actors.php taxonomy-directors-php 我希望仅在功能处于活动状态时使用它们,例如if ($theme_comments == 1) { //load taxonomy-actors.php } else { //don\'t load taxonomy-actors.php&#