Block Links in Gravity Forms Text Fields
Rejects Gravity Forms submissions that put web links in name, text or paragraph fields, which removes most link-spam without a captcha.
Opens WPIDE on
Code
$wpide_gf_link_field_types = array( 'text', 'textarea', 'name' ); // Field types to check.
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) use ( $wpide_gf_link_field_types ) {
if ( ! $result['is_valid'] || ! in_array( $field->type, $wpide_gf_link_field_types, true ) || false !== strpos( ' ' . $field->cssClass . ' ', ' allow-links ' ) ) {
return $result;
}
$text = is_array( $value ) ? implode( ' ', $value ) : (string) $value;
if ( preg_match( '#(https?://|www\.|\[url|<a\s)#i', $text ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please remove links from this field.';
}
return $result;
}, 10, 4 );
Notes
Website fields are never checked; add the CSS class allow-links to a field that should accept links.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.