Send Comments With Too Many Links to Spam
Marks a comment as spam when it contains more links than you allow, catching most link-dropping spam before it reaches the moderation queue.
Opens WPIDE on
Code
// Most links a genuine comment may contain.
$wpide_max_comment_links = 2;
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) use ( $wpide_max_comment_links ) {
if ( is_wp_error( $approved ) || 'spam' === $approved || 'trash' === $approved || ! empty( $commentdata['user_id'] ) ) {
return $approved;
}
$links = preg_match_all( '#(https?://|www\.)#i', (string) $commentdata['comment_content'] );
return $links > $wpide_max_comment_links ? 'spam' : $approved;
}, 20, 2 );
Notes
Logged-in users are never affected; check the Spam folder now and then for false positives.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.