Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Automatically approve comments for people with more than 5 comments approved already
function filter_handler( $approved , $commentdata ) {
// get the comment author name and email and check how many comments that person has approved.
global $wpdb;
$author = $commentdata[ 'comment_author' ];
$email = $commentdata[ 'comment_author_email' ];
$comments_approved = $wpdb->get_var( "SELECT COUNT(comment_approved) FROM $wpdb->comments WHERE comment_author = '$author' AND comment_author_email = '$email' and comment_approved = '1' LIMIT 1" );
// if the author already has more than 5 comments approved, set this to approved, otherwise set as pending
if ( 5 < $comments_approved ) {
$approved = 1;
else {
$approved = 0;
// return the approved value
return $approved;
add_filter( 'pre_comment_approved' , 'filter_handler' , '99', 2 );

This comment has been minimized.

Copy link
Owner Author

@Stephen-Cronin Stephen-Cronin commented Sep 23, 2015

The DB query should probably be prepared - although it's based on WP Core which doesn't use prepare and I don't think it can be exploited (you can inject bad stuff but I couldn't make it break out of the quotes).

Still, if I was using this in a plugin or production, I'd run it through $wpdb-prepare....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment