Wordpress REST API rest_comment_invalid_author Sorry, you are not allowed to edit 'author' for comments
I'm fairly new to Wordpress REST API, but I have an Angular application which needs to take user's input and then create a comment anonymously. To start off, we can get rid of the rest_comment_login_required
error message by modifying the Wordpress REST API function located in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
file. The following line allows anonymous commenting:
$allow_anonymous = apply_filters( 'rest_allow_anonymous_comments', true, $request );
In my Wordpress REST API, sending a GET
request to http://localhost/wordpress/wp-json/wp/v2/users/1
returns:
{
id: 1,
name: root,
url: http://localhost/wordpress,
description: ,
link: http://localhost/wordpress/author/root/,
slug: root,
avatar_urls: {
24: http://1.gravatar.com/avatar/d11662ae04f559ce691c5b1b4ff906f7?s=24d=mmr=g,
48: http://1.gravatar.com/avatar/d11662ae04f559ce691c5b1b4ff906f7?s=48d=mmr=g,
96: http://1.gravatar.com/avatar/d11662ae04f559ce691c5b1b4ff906f7?s=96d=mmr=g
},
meta: [ ],
_links: {
self: [
{
href: http://localhost/wordpress/wp-json/wp/v2/users/1
}
],
collection: [
{
href: http://localhost/wordpress/wp-json/wp/v2/users
}
]
}
So I only have one user currently in my database, who is called root
.
If I simulate a request to the API with Postman and send a POST
request to http://localhost/wordpress/wp-json/wp/v2/comments
while supplying a raw JSON body thata looks like
{
content: Lorem Ipsum is simply dummy text of the printing and typesetting industry,
date: 2021-08-04T22:24:09,
post: 1,
author: 1,
author_name: root,
author_url: http://localhost/wordpress
}
then I get an error
{
code: rest_comment_invalid_author,
message: Sorry, you are not allowed to edit 'author' for comments.,
data: {
status: 401
}
}
What is the meaning of this error and how do I fix this?