Skip to content

Instantly share code, notes, and snippets.

@them-es
Last active November 22, 2020 05:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save them-es/7c8a120ac868f5c4fc474e53a1266c83 to your computer and use it in GitHub Desktop.
Save them-es/7c8a120ac868f5c4fc474e53a1266c83 to your computer and use it in GitHub Desktop.
Ninja Forms: Server side spam protection using WordPress comment blacklist keys
<?php
/**
* [Update] This Gist has been integrated into the WordPress Plugin "I don't like Spam!" which supports more WordPress Contact Forms:
* https://wordpress.org/plugins/i-dont-like-spam
*
* Ninja Forms: Server side spam protection making use of the WordPress Comment Blocklist
* https://developer.ninjaforms.com/codex/custom-server-side-validation
*
* Enter your blocklist here: Settings > Discussion > Comment Blocklist
* https://developer.wordpress.org/reference/functions/wp_blacklist_check
* https://raw.githubusercontent.com/splorp/wordpress-comment-blacklist/master/blacklist.txt
*/
function my_theme_ninja_forms_submit_data( $form_data ) {
$bad_words = explode( "\n", strtolower( trim( get_option( 'blacklist_keys' ) ) ) );
foreach ( $form_data['fields'] as $field ) {
// Field settings, including the field key and value.
$field_value = wp_strip_all_tags( strtolower( $field['value'] ) );
$field_id = esc_attr( $field['id'] );
foreach ( $bad_words as $bad_word ) {
$bad_word = trim( $bad_word );
// Skip empty lines.
if ( empty( $bad_word ) ) {
continue;
}
if ( false !== strpos( $field_value, $bad_word ) ) {
$form_data['errors']['fields'][$field_id] = __( 'This field contains a word that has been blocked.', 'my-theme' );
}
}
}
return $form_data;
}
add_filter( 'ninja_forms_submit_data', 'my_theme_ninja_forms_submit_data' );
@them-es
Copy link
Author

them-es commented Jun 22, 2020

This Gist has been integrated in the WordPress Plugin "I don't like Spam!" which supports more WordPress Contact Forms (i.e. Ninja Forms, Caldera Forms and WPForms).

Feel free to download the Plugin from the WordPress Plugin Directory:
https://wordpress.org/plugins/i-dont-like-spam

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment