The WordPress REST API Version 2 is brand new in the developer world which means that the documentation is extremely limited. Hopefully this can help a little for others trying to debug problems like this.
Posting comments to WordPress via the REST API Version 2 is actually relatively straight forward once you figure out how to do this. For those looking for a quick solution, using POST on the following URL will do just that, post a comment on this blog post you are reading right now.
Now for a few comments on the technical aspects to understand how this works.
Read the WordPress API documentation under the Create a Comment heading. As you will see, the documentation is minimal to say the least. It’s something the WordPress Core team are working on, so stick with it.
Essentially though, there are various query string parameters you can append to the request to send data into WordPress as a comment. It’s important to note that this is a POST request not a GET request. GET requests on this URL will not work. You need to use a tool such as Advanced REST client which allows you to POST data to API URLs which is extremely handy.
You will no doubt have debugging to do when you are first testing this as nothing ever goes to plan. Make sure you have comments turned on at the global WordPress level under the Settings > Discussions tab and also make sure that you have comments turned on for individual posts as sometimes these have been disabled. It’s always best to show these on your website too.
As with anything comment related with WordPress, make sure you are using the Akismet plugin to block any spam as this is a real nightmare on WordPress without Akismet.
There are lots of extremely useful uses for using this, we’ve been recently using this to post comments from a mobile app into WordPress which is used as a comment moderation system which means we don’t have to go and build that side of the functionality.
Make sure you are escaping the content which is included in the query string too and keep an eye out for any rogue spaces or special characters which may be breaking your POST request if this hasn’t worked. The usual things to check which you often miss J
When you successfully post a comment, you will see this waiting in your comment queue;
Then when you approve the comment you will see this displays correctly on your website as you would expect;
Have a go yourself. Customise the above URL with your own details and comments within the parameters and I’ll publish any successful comments. This handy URI Encoder / Decoder tool may come in useful when writing a comment or your name if it includes spaces or special characters, something I haven’t got around to migrating over to this site just yet.
Your Comment Here
test comment
test to the test
comment asdasdasdfkaojfpöslkDJFOPAEHRJVPOAKWVISHFOPKEDSAIAHFD
test1
Testing through body
Hi hi
COmemme
Your Comment Here-2
testCommentWhoot
test
Testy
some thing
some thing
Test
asdfasd
test comment
test comment
test
can i comment here?
cant get it to work. it says Invalid parameter(s): author
Hi Jeuel, your comments have come through on this end 🙂 I can’t see anything that has changed in the REST API end points that would cause something different for you. Looks like you got it working though.
Regards,
Michael
It looks like ‘author’ require the ID of a specific user, and hence why you would need to be logged in to post on behalf of a specific user. Try changing this to ‘author_name’ which accepts a String, http://v2.wp-api.org/reference/comments/
i tried replacing author with 0 (zero). and i got this result:
“message”: “Sorry, you must be logged in to comment.”
what the test
hi
i tried to POST this:
https://www.contradodigital.com/wp-json/wp/v2/comments?author=0&author_email=admin@admin.com&author_name=alireza&content=salam&post=1604252
but it says to me:
“message”: “Sorry, you must be logged in to comment.”
what should i do?
plz help me.
Hi Alireza,
It looks like there has been a change as of WordPress 4.7.0 which now disables anonymous posting of comments through the REST API, https://developer.wordpress.org/reference/classes/wp_rest_comments_controller/create_item_permissions_check/. To get around this, add the following code to your functions.php file;
function filter_rest_allow_anonymous_comments() {
return true;
}
add_filter('rest_allow_anonymous_comments', 'filter_rest_allow_anonymous_comments');
I’ve just added this to our site so if you want to test posting a comment again, it should come through this time. I’ve just tested this myself and everything is working again.
Regards,
Michael
Even i did the same, what you asked for but it still shows “login error”, what should i do now.
Your Comment Here
Your Comment Here
Mycomment
Your Comment Hereee
Cuando hago el post a este blog (Este articulo) todo funciona bien pero cuando trato de hacerlo a mi blog que dice: “Lo siento, tiene que estar conectado para comentar.” status 401.
Le he puesto que pueda comentar sin que este logueado en la configuracion de mi blog y funciona pero el post sigue diciendo lo mismo. Alguien tiene alguna idea de porque me esta pasando esto?
When I post to this blog (This article) everything works fine but when I try to do it to my blog that says “Sorry, you have to be logged in to comment.” Status 401.
I have since I can comment without this login in the configuration of my blog and it works but the post keeps saying the same thing. Does anyone have any idea why this is happening to me?
Hi Luis,
See my previous reply to @Alireza above which should have a solution to that problem you’re experiencing. 🙂
Regards,
Michael
Your Comment Here
Hello
Your Comment Here
Thank you very much , but you should update the article to include the bug that appears from 4.7+ that you have mentioned it in the comments
qwe
qwe123
Your Comment Here
Your Comment Here
Your Comment Here
thats not work again
code : rest_forbidden_param
message: Query parameter not permitted:
status: 401
qwerty
b
Your Comment Here
TESTING$$$$$$
Your Comment Hereeeeeeeeeeeeee
Your
Testing123
Howdy Here
sure thing honey!
Hi There Here
the codes doesn’t work in wordpress 4.7
Check the solution mentioned in a few of the previous comments. It should work for you following those guides.
test
Hello,
I just posted trying this form after trying the rest param but it seems like only the form works.
The rest param give a status 401 forbidden (the same issue than on August 3, 2017 at 2:20 pm).
Would you have hint as it used to re-work the 12th?
Your Comment Here
test
instead of query string if i am passing these parameters as json object its thorowing exception (400) “Invalid JSON body passed.”.
var data={
author: null,
author_email: ‘myemail.com’,
author_ip: null,
author_name: ‘debasis’,
author_url: null,
author_user_agent: null,
content: ‘Very%helpful%info’,
date: null,
date_gmt: null,
parent: null,
post: blogid,
status: null,
meta:null
}
console.log(data);
$.ajax({
type: ‘post’,
url: ‘mydomain/wp-json/wp/v2/comments’,
contentType: ‘application/json; charset=utf-8’,
headers: {
“Authorization”: “Bearer ” + localStorage.getItem(‘_token’)
},
data: data,
success: function (comments) {
console.log(comments);
},
error: function (err) {
console.log(err);
}
});
if i am passing those parameters as query string its working fine
but why ???
You’re not sending valid JSON data 🙂 That’s a simple fix.
can you please tell me where i am going wrong.
test comment
Its not working. I tried it here as well as on my site and i’m getting this error.
“{“code”:”rest_forbidden_param”,”message”:”Query parameter not permitted: author_email”,”data”:{“status”:401}}”
I already added php code in functions.php to allow anonymous comments via api. Can you please help.
Off the top of my head, I’m not sure. If I’d have to guess based on what you’ve said, it is likely something around the anonymous comments – . But i’d need to take a look to see what could be going on there… https://www.contradodigital.com/can-you-help/ – First port of call would be to take a look at, https://developer.wordpress.org/reference/hooks/rest_allow_anonymous_comments/, as by default you wouldn’t want to allow anonymous comments through a REST API due to this being open to spam.
Hope that helps.
Regards,
Michael
Thanks for response.
I already added that code which you shared in earlier post. Also same code is being shared on stackoverflow.
There are quite a bit “Your Comment Here” type of comments above on this post.
I’m guessing those are being made by simply opening the URL you shared in post content. That’s how i’m trying to test here as well as on my site.
“https://mydomain.com/wp-json/wp/v2/comments?author_name=S%20Ali&author_email=myemail@hotmail.com&content=Your%20Comment%20Here&post=13753”
Can you tell me if i’m doing it right?
Hello,
I would like to know if you succeeded.
If yes, how ?
Thank you
Hello, I’m too facing the same issue. Have included the anonymous allowing code too.
{“code”:”rest_forbidden_param”,”message”:”Query parameter not permitted: author_email”,”data”:{“status”:401}}
Please let us know how to solve this?
test not coming up
Your Comment Here
Your Comment Here
Your Comment Here
Your Comment Here
Your Comment Here
Testing through bosy
Testing through body
Does this thing work? Last one was marked off…
test comment via python requests with params in a json
zak here for test
my cmnt
test comment
console.log(“test”);
Thank god that didn’t work… phew! 😉 …..XSS (Cross Site Scripting) 🙂 Thankfully, WordPress protects against this kind of stuff by default which is pretty cool 😀
hello test
Great%Job%with%the%post!
Hello there!
test gmjkwsij
Mero hawa comment
testing
test2
Umesh Testing
Your Comment Here
Testing through bosy
Test Comment
There are lots of extremely useful uses for using this, we’ve been recently using this to post comments from a mobile app into WordPress
Yes this is a really handy thing to be able to do. Take a look at this case study of how we’ve used the WordPress REST API to do a very similar thing. So much potential. https://www.contradodigital.com/case-studies/mend-muslim-engagement-development-case-study/
this is a comment
Your Comment Here
sadasd
said s comment
this is my comment
my second comment
Comments are superflusous
http://test-(1).xls
comment like above is getting breaking, can you please suggest the right filter to make it enter properly in comments db
http://test-(1).xls
for this .xls is coming as text and remaining part is coming as link
how to fix this to get entire url as link? is there any way to achieve it.
This is a simple one…. write a valid URL 😀
https://en.wikipedia.org/wiki/URL#Syntax
http://test-(1).xls
http://test-(1).com/example.xls
http://test-1.com/example.xls
http://test-2.com/example.xls
help the world
My cat stinx
Shampoo is the answer…
This is a nice comment.
here jenna
test
This is a test comment.
test