# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: gravity-forms, spam, validation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/gf-no-links-in-fields/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Block Links in Gravity Forms Text Fields" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
