0

I want my bloggers to be able to delete comments via the front end instead of the standard way via the WP dashboard. I wrote a function custom_delete_post_comment() which deletes a comment with a given ID.

function custom_delete_post_comment() {
   $comment_id = comment_ID();
   wp_delete_comment( $comment_id, true ) 
}

As you can see, my function uses WordPress' wp_delete_comment() function.

I plan on having a button next to each comment which when clicked will run the delete function I wrote hence removing the comment.

I have come up with a solution using the $_POST approach. My question is how do I modify my code so that the page reloads to reflect the fact that the comment has been deleted?

<?php if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
    set_query_var( 'commentid1', $_POST['commentid'] );
    wp_delete_comment( get_query_var( 'commentid1'), true );    
};
?>

<form class="delete-comment" action="" method="post">
   <input type="hidden" name="commentid" value="<?php comment_ID() ?>" /> 
   <input type="submit" value="Delete" title="Delete" class="btn" />
</form>
brasofilo
  • 25,496
  • 15
  • 91
  • 179
henrywright
  • 10,070
  • 23
  • 89
  • 150
  • $_POST or Ajax. Check [this](http://stackoverflow.com/a/13614297/1287812) – brasofilo Sep 24 '13 at 11:45
  • Thanks @brasofilo I used the $_POST approach. I've updated my question above with a partial solution. The comment is being deleted from the database, I just now need to know how to show the user that the comment has been deleted e.g how do I have the page reload after the form is submitted? – henrywright Sep 24 '13 at 12:36
  • The form will have to encapsulate all comments, no? The checkbox ID must be the comment ID, like `name="comm_id[ID-NUMBER]`. Then `isset($_POST['comm_id'])` and iterate through it. – brasofilo Sep 24 '13 at 13:01
  • Are you opposed to using something like jQuery to hide the comment after the delete goes through? – Jim Sep 24 '13 at 13:02
  • @brasofilo yes, the form will be used within the comments loop. I'm thinking a checkbox to delete multiple comments will be overkill as bloggers shouldn't want to delete too often hopefully. Therefore no need to loop through – henrywright Sep 24 '13 at 13:24
  • @Jim thanks for the jQuery hide idea, i'll give that a go now – henrywright Sep 24 '13 at 13:24

0 Answers0