Skip to content

Instantly share code, notes, and snippets.

@them-es

them-es/functions.php

Last active Jun 24, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@them-es 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
You can’t perform that action at this time.