Force Post Author Comments to Moderation in WordPress

By default in WordPress when an author of a post comments on their own posting the comment is automatically approved. This makes sense when authors are associated with the site, have access to admin, etc.

There are cases where content from the general public is used to create posts and while the user is logged in and set as the author it’s still responsible to moderate their comments.

The below snipppet checks to see if the user id found in $commentdata has permissions to moderate comments. If not, we force to moderation, if so, we approve it.

//force authors comments on own post to be moderated by admin
add_filter('pre_comment_approved', 'thisbailiwick_pre_comment_approved', null, 2);
function thisbailiwick_pre_comment_approved($approved, $commentdata) {
    //if the use cannot moderate commments
    if (!user_can($commentdata['user_ID'], 'moderate_comments')) {
        $approved = 0;
    }else{
        $approved = 1;
    }

    return $approved;
}

Leave a Reply

Your email address will not be published. Required fields are marked *