Title: Save/Edit a comment
Last modified: August 21, 2016

---

# Save/Edit a comment

 *  [juandbb](https://wordpress.org/support/users/juandbb/)
 * (@juandbb)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/)
 * Hello,
 * I cannot figure how to edit/post a comment. May anybody help me please?
 * Thank you very much in advance.
 * [https://wordpress.org/plugins/json-rest-api/](https://wordpress.org/plugins/json-rest-api/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Ryan McCue](https://wordpress.org/support/users/rmccue/)
 * (@rmccue)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/#post-5008881)
 * This is not currently possible, sorry! We’re tracking this issue on GitHub as
   [issue 86](https://github.com/WP-API/WP-API/issues/86).
 *  [aryanduntley](https://wordpress.org/support/users/dunar21/)
 * (@dunar21)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/#post-5008882)
 * There are three ways as I see it. You can hook into the edit post method in one
   of two ways (json_insert_post or json_pre_insert_post), or you can extend the
   api with WP_JSON_CustomPostType or WP_JSON_Posts. Extending using posts, see 
   class json media. Extending using custom post type see json page.
 * The downfall to using json_insert_post is that a new record (revision) of the
   post will be created and with a lot of comments, this may be undesirable. The
   downside to json_pre_insert_post is that you would have to return a WP Error 
   object. That can be hacked by returning something like : `return new WP_Error('
   json_comment_created', __('Your Comment ID ' . $commentID . 'Was Created.'), 
   array( 'status' => 201));`
 * [http://codex.wordpress.org/Function_Reference/wp_insert_comment](http://codex.wordpress.org/Function_Reference/wp_insert_comment)
 *     ```
       function put_a_comment($post, $data, $update){
        if($update){
         /*MAKE SURE COMMENT IS NOT SPAM, IS VALID, IS ESCAPED, WHATEVER*/
         $data = array(
            'comment_post_ID' => $post["ID"],
            {RESTOF_NEEDED_COMMENT_DATA}
         );
         $cmid = wp_insert_comment($data);
        }
       }
   
       add_action( 'json_insert_post', 'put_a_comment', 10, 3);
   
       function put_a_comment_errr($true, $post, $data, $update){
        if($update){
         /*MAKE SURE COMMENT IS NOT SPAM, IS VALID, IS ESCAPED, WHATEVER*/
         $data = array(
            'comment_post_ID' => $post["ID"],
            {RESTOF_NEEDED_COMMENT_DATA}
         );
         $cmid = wp_insert_comment($data);
         return new WP_Error( 'json_comment_created', __('Your Comment ID ' . $cmid . 'Was Created.'), array( 'status' => 201));
        }
       }
   
       add_filter('json_pre_insert_post', 'put_a_comment_errr', 10, 4);
       ```
   
 * Extending is probably the best way to go. Then you can set your routes to `'/
   posts/(?P<id>\d+)/comment' => array(array( array( $this, 'get_comments' ), WP_JSON_Server::
   EDITABLE | WP_JSON_Server::ACCEPT_JSON),),`
 * Either way you do it, you have to make sure you send the json data. Something
   like “comment_author”:”blahblah”,”comment_body”:”blahblahblah” etc… then those
   params will be available in your data variable.
 *  Plugin Author [Ryan McCue](https://wordpress.org/support/users/rmccue/)
 * (@rmccue)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/#post-5008883)
 * > There are three ways as I see it. You can hook into the edit post method in
   > one of two ways (json_insert_post or json_pre_insert_post), or you can extend
   > the api with WP_JSON_CustomPostType or WP_JSON_Posts. Extending using posts,
   > see class json media. Extending using custom post type see json page.
 * The post endpoints shouldn’t need to be involved here. While you can add your
   own custom endpoints, it’s important to keep in mind that these may not have 
   compatibility with the eventual version included in the API.
 * Also note that dunar21’s example code from above is not safe to run in production,
   as it doesn’t touch on sanitisation. A big part of why it takes us a long time
   to add these new API endpoints is because it takes time to go over every available
   field and ensure they’re sanitised properly. 🙂
 *  Thread Starter [juandbb](https://wordpress.org/support/users/juandbb/)
 * (@juandbb)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/#post-5008884)
 * Thank you very much to both of you

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Save/Edit a comment’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/json-rest-api_2e3641.svg)
 * [WP REST API (WP API)](https://wordpress.org/plugins/json-rest-api/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/json-rest-api/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/json-rest-api/)
 * [Active Topics](https://wordpress.org/support/plugin/json-rest-api/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/json-rest-api/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/json-rest-api/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [juandbb](https://wordpress.org/support/users/juandbb/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/saveedit-a-comment/#post-5008884)
 * Status: not resolved