Use the Comment Blocklist for Contact Form 7
Marks Contact Form 7 submissions as spam when they contain any word, IP address or email from WordPress's own Disallowed Comment Keys list, so one blocklist protects comments and forms.
Opens WPIDE on
Code
add_filter( 'wpcf7_spam', function ( $spam, $submission = null ) {
if ( $spam || ! $submission ) {
return $spam;
}
$keys = trim( (string) get_option( 'disallowed_keys' ) );
if ( '' === $keys ) {
return false;
}
$text = (string) $submission->get_meta( 'remote_ip' ) . ' ' . (string) $submission->get_meta( 'user_agent' );
foreach ( (array) $submission->get_posted_data() as $value ) {
$text .= ' ' . ( is_array( $value ) ? implode( ' ', $value ) : (string) $value );
}
foreach ( preg_split( '/\R/', $keys ) as $word ) {
$word = trim( $word );
if ( '' !== $word && false !== stripos( $text, $word ) ) {
$submission->add_spam_log( array( 'agent' => 'wpide-disallowed-keys', 'reason' => 'Matches the Disallowed Comment Keys list.' ) );
return true;
}
}
return false;
}, 10, 2 );
Notes
Manage the list under Settings > Discussion > Disallowed Comment Keys, one entry per line.
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.