Last active
September 23, 2015 02:33
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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....