Last active
September 7, 2023 17:09
-
-
Save tamsininnit/a3b6ae970facdc81ec5163c2c45a31da to your computer and use it in GitHub Desktop.
Blocks words in Gravity Forms (WordPress Plugin) based on the disallowed words in Settings > Discussion (where you'd usually block words for use in comments)
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
/* Remember to set your list of blocked words in WordPress > Settings > Discussion > Disallowed Comment Keys */ | |
/* Gravity Forms Blocked Words */ | |
add_filter( 'gform_validation', 'validate_disallowed_keys' ); | |
function validate_disallowed_keys( $validation_result ) { | |
$form = $validation_result['form']; | |
$blacklist_keys = get_option( 'blacklist_keys' ); | |
foreach ( $form['fields'] as &$field ) { | |
// Get the field value | |
$field_value = rgpost( 'input_' . $field->id ); | |
// Check if the field value is disallowed | |
if ( wp_check_comment_disallowed_list( '', $field_value, '', '', '', '', $blacklist_keys ) ) { | |
// Mark the form as invalid | |
$validation_result['is_valid'] = false; | |
// Mark the field as invalid and add a custom validation message | |
$field->failed_validation = true; | |
$field->validation_message = 'We do not accept messages about link building services, content creation services or guest posts.'; | |
} | |
} | |
// Return the modified validation result | |
return $validation_result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use wp_check_comment_disallowed_list (); instead of wp_blacklist_check ();