我正在尝试使用基本身份验证向WordPress(4.7.0)发布评论,如documentation和WP 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}}
我激活了基本身份验证插件,然后将此调用转换为经过身份验证的调用,对吗?
答案 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)
转到rootfolder/wp-includes/rest-api/endpoints
打开文件:class-wp-rest-comments-controller.php
更改行:
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', false , $request );
到
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', true, $request );
仅此而已