Skip to content

Instantly share code, notes, and snippets.

@them-es them-es/functions.php
Last active May 29, 2020

Embed
What would you like to do?
Ninja Forms: Server side spam protection using WordPress comment blacklist keys
<?php
/**
* Ninja Forms: Server side spam protection using WordPress comment blacklist keys
*
* https://developer.ninjaforms.com/codex/custom-server-side-validation
*
* Enter your blacklisted words 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
You can’t perform that action at this time.