Warning: preg_replace(): Compilation failed: escape sequence is invalid in character class at offset 4 in /home/customer/www/theunixtips.com/public_html/wp-content/plugins/resume-builder/includes/class.resume-builder-enqueues.php on line 59

WordPress : Restrict Comment changes to Admin and creator for 24 hours

Wordpress allows the “creator” of the post to edit anyone’s comment on that post (citation needed). And if the user has admin rights he can edit anyone’s comments. A requirement came across where I needed to limit the users to only edit their comments. I did not want them to play around with words of others on their posts. Also I did not want them to go back and edit their old comments (so they do not create unnecessary confusions for everyone). Here is a quick and dirty change that can limit the user’s capability on what they can do to comments. This shall be put in a plugin which will be the next thing I will take on.

Here are the basic requirements.

  1. Admin user can edit anyone’s comments.
  2. Only the user that posted the comment shall be able to edit the comment.
  3. Editing should be allowed for only certain time period (lets say 24 hours), after which that comment cannot be edited. This limit does not apply to Admin obviously.

I ended up modifying the comments.php in the themes directory to include following check. The theme in question was calling “edit_comment_link” for each comment as below.

  edit_comment_link(__('Edit', 'sandbox'), ' ', '');

I modified it to following.

if (current_user_can( 'manage_options' )  // Admin allowed
  || ( ($comment->user_id == $user_ID)    // User matches
  && ( $time_ago < (60 * 24 * 60)) )      // Live for 24 hours
  edit_comment_link(__('Edit', 'sandbox'), ' ', '');