5

I am using an AngularJS application with WordPress backend. I am able to retrieve comments using WP REST API v2., using the following url

/wp-json/wp/v2/comments?post=1324

But is there a way to post comments in WP REST API v2. I was unable to find anything in documentation. Thanks.

2
  • In the documentation there is a section titled "Create a comment". You can start there and come back if you have problems to use it.
    – cybmeta
    Jan 14, 2016 at 7:20
  • The REST endpoint for comments is inconsistent in that the schema for content is different on the outgoing (GET side) vs. incoming (POST/PUT). When you read you get an object with raw/rendered properties. When you add/update a comment you are expected to provide a plain string for the content.
    – kodbuse
    Aug 14, 2016 at 0:20

3 Answers 3

4

Similar problem, I was getting back:

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

Looking for that code 'rest_comment_login_required' this is one of the first results: WP_REST_Comments_Controller::create_item_permissions_check.

So from v4.7.0 there is a filter called 'rest_allow_anonymous_comments' returning false by default. So you cannot comment by default even though your Wordpress settings indicate otherwise.

You can turn it back on by adding:

function filter_rest_allow_anonymous_comments() {
    return true;
}
add_filter('rest_allow_anonymous_comments','filter_rest_allow_anonymous_comments');
3
  • 2
    You can also use the __return_true callback
    – birgire
    Dec 19, 2016 at 8:51
  • The code provided doesn't work above, you've got the filter and callback the wrong way round :-) developer.wordpress.org/reference/functions/add_filter. This is the correct code... function filter_rest_allow_anonymous_comments() { return true; } add_filter('rest_allow_anonymous_comments', 'filter_rest_allow_anonymous_comments'); Mar 6, 2017 at 11:11
  • You're right: I've updated the code.
    – Sjeiti
    Mar 6, 2017 at 13:27
3

Take a look at the comment docs - http://v2.wp-api.org/reference/comments/

And the following syntax:

https://www.example.com/wp-json/wp/v2/comments?author=Your%20Name%20Here&author_email=your-email-address@website-address-here.com&author_name=Your%20Name%20Here&content=Your%20Comment%20Here&post=1604252

1

Here you go: http://www.contradodigital.com/2016/04/06/post-comments-wordpress-rest-api-version-2/

Just had to do this myself, the documentation in the official WordPress API v2 isn't the best at the minute.

1
  • 4
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
    – Gabriel
    Apr 6, 2016 at 21:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.