使用基本身份验证在Wo​​rdPress 4.7.0上发表评论时401(rest_comment_login_required)

时间:2017-06-12 12:21:37

标签: wordpress authentication

我正在尝试使用基本身份验证向WordPress(4.7.0)发布评论,如documentationWP REST API: Setting Up and Using Basic Authentication中所述。

然而,我一直收到401错误。

{"code":"rest_comment_login_required","message":"Sorry, you must be logged in to comment.","data":{"status":401}}

我激活了基本身份验证插件,然后将此调用转换为经过身份验证的调用,对吗?

2 个答案:

答案 0 :(得分:1)

您需要为主题的functions.php文件添加一个过滤器:

add_filter( 'rest_allow_anonymous_comments', function ( $allow_anonymous, $request ) {
    // ... custom logic here ...
    return true; // or false to prohibit anonymous comments via post
}, 10, 2 ); 

文档:https://developer.wordpress.org/reference/hooks/rest_allow_anonymous_comments/

答案 1 :(得分:0)

  1. 转到rootfolder/wp-includes/rest-api/endpoints

  2. 之类的WordPress根目录
  3. 打开文件:class-wp-rest-comments-controller.php

  4. 更改行:

    $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false , $request );
    

    $allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', true, $request );
    

仅此而已