Skip to content

Instantly share code, notes, and snippets.

@Stephen-Cronin
Last active September 23, 2015 02:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Stephen-Cronin/42f619ef9f7580924e0c to your computer and use it in GitHub Desktop.
Automatically approve comments for people with more than 5 comments approved already
<?php
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 );
?>
@Stephen-Cronin
Copy link
Author

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