Last active
November 22, 2020 05:17
-
-
Save them-es/7c8a120ac868f5c4fc474e53a1266c83 to your computer and use it in GitHub Desktop.
Ninja Forms: Server side spam protection using WordPress comment blacklist keys
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 | |
/** | |
* [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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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