- Contributors: joaquiminteresting
- Tags: wp, rest, api, rest api, comment, json, rest api comments, wordpress comments, posts, post comments, reply comments
- Requires at least: 5.5
- Tested up to: 5.8.1
- Requires PHP: 7.0
- Stable tag: 1.0.2
- License: GPLv2 or later
- License URI: https://www.gnu.org/licenses/gpl-2.0.html
REST API Comment adds in the 'Comment creation' function to the Wordpress REST API.
If you wish to 'Create comments' using REST API, without exposing Administrator credentials to the Front-End application, you are at the right place. Since WordPress 4.7, REST API was natively included in WordPress.
In order to 'Create a comment' , the authentication for a user with 'Administrator' role is required. While this is a deliberately done for security reasons, such implementation makes it very hard for Front-End applications to implement a simple 'Post Comment' or 'Reply Comment' function.
This plugin fulfils such requirement by extending the existing WordPress REST API endpoints.
Minimum PHP version: 7.0
Minimum Wordpress version: 5.5
Download the this repository and install it like any other WordPress plugin.
Or clone this repo into your WordPress installation into the wp-content/plugins folder.
After the installation activate the plugin through the 'Plugins' menu in WordPress
When this plugin is installed one new endpoint is added to the wp/v2 namespace.
| Endpoint | HTTP Verb | Permalinks |
|---|---|---|
| */wp-json/wp/v2/comments/create | POST | enabled |
| */?rest_route=/wp/v2/comments/create | POST | disabled |
To create a comment using REST API, send a POST request to:
/wp-json/wp/v2/comments/create- if permalinks is enabled on your wordpress website.
Or
/?rest_route=/wp/v2/comments/createif permalinks is not enabled on your wordpress website.
With a JSON body, as shown bellow:
{
"post": "Post ID",
"author_name": "Comment Author's name",
"author_email": "Comment Author's email",
"content": "Comment content"
}The content may also be send as an object:
{
"post": "Post ID",
"author_name": "Comment Author's name",
"author_email": "Comment Author's email",
"content": {
"raw":"Comment content"
}
}Set header to:
content-type: application/json
If successful, you should receive a response with the data of the created comment:
{
"id": "[comment id]",
"status": "[comment status]",
"message":"[server response message]"
}In response header the status code should be:
HTTP 201 CreatedTo reply a comment you just need to add the field parent for the parent comment to the JSON body
{
"post": "Post ID",
"author_name": "Comment Author's name",
"author_email": "Comment Author's email",
"content": "Comment content",
"parent":"Comment parent ID"
}Note: Ensure the parent is a comment id that belongs to the post informed in the field post. The comment parent post id must match the post id otherwise the following error will be shown:
{
"code": "rest_post_mismatch_parent_post_id",
"message": "Post ID and Parent post ID does not match",
"data": {
"status": 400
}
}If you're planning on using your WordPress news website/blog as a Backend, and you're consuming RESTful api, you'll most probably need to Create comments and Reply comments via REST API. This is precisely what this plugin does.
Great question! For the time being, this plugin just provides the same experience any wordpress site provides by default witch is allowing any one to comment a post requiring basic infos such as: name, email address and the content, without authentication. All security was followed based on the wordpress core code.
Please create a ticket on the support team or open an issue in github repository. We'll get back to you as soon as possible.
- An sample REST API POST request using REST API Comment.
- It prevents comments for posts with comment status 'closed'
- It prevents comments for posts with post status different from 'publish'
- Now it requires at least wordpress version 5.5
- Now it requires at least wordpress version 4.7
- Response was updated
- README was Updated
- Bug fixed
- Initial Release
- Create comment
- Reply comment
If there is any thing to say about the plugin fill free to contact us. We'll to get in touch.