Search code examples
wordpresscomments

Wordpress increase number of previously approved comment


In the Wordpress -> Settings -> Discussion there a parameter called "Comment author must have a previously approved comment"

The users must be have a comment approved before to auto approving the after comments. How can I increase this parameter, this mean the users must be have 5 comment approved before?

Thanks


Solution

  • Use below code snippet in your functions.php based on the value of Comment author must have a previously approved comment, it will check whether the user has 5 commented approved or not. If not then it will return false otherwise it will return true.

    function pre_comment_approved_callback($approved, $commentdata){
    global $wpdb;
        $author = $commentdata['comment_author'];
        $email = $commentdata['comment_author_email'];
        $comment_type = $commentdata['comment_type'];
        $mod_keys = trim( get_option( 'moderation_keys' ) );
    
        if ( 1 == get_option( 'comment_previously_approved' ) ) {
            if ( 'trackback' !== $comment_type && 'pingback' !== $comment_type && '' !== $author && '' !== $email ) {
                $comment_user = get_user_by( 'email', wp_unslash( $email ) );
                if ( ! empty( $comment_user->ID ) ) {
                    $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_approved) FROM $wpdb->comments WHERE user_id = %d AND comment_approved = '1' LIMIT 5", $comment_user->ID ) );
                } else {
                    // expected_slashed ($author, $email)
                    $ok_to_comment = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_approved) FROM $wpdb->comments WHERE comment_author = %s AND comment_author_email = %s and comment_approved = '1' LIMIT 5", $author, $email ) );
                }
                
                if ( ( 5 == $ok_to_comment ) &&
                    ( empty( $mod_keys ) || false === strpos( $email, $mod_keys ) ) ) {
                        return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        }
    
        return $approved;
    }
    add_filter('pre_comment_approved', 'pre_comment_approved_callback', 10, 2);