Skip to content

Instantly share code, notes, and snippets.

@jodzeee
Created January 6, 2025 21:09
Show Gist options
  • Save jodzeee/dd5e65759ae0d386d369c95de4d69d5b to your computer and use it in GitHub Desktop.
Save jodzeee/dd5e65759ae0d386d369c95de4d69d5b to your computer and use it in GitHub Desktop.
Gravity Forms - send to spam if URLs or email in text field
<?php
add_filter( 'gform_entry_is_spam', 'filter_gform_entry_is_spam_urls_and_emails', 11, 3 );
function filter_gform_entry_is_spam_urls_and_emails( $is_spam, $form, $entry ) {
if ( $is_spam ) {
return $is_spam;
}
$field_types_to_check = array(
'hidden',
'text',
'textarea',
);
foreach ( $form['fields'] as $field ) {
// Skipping fields which are administrative or the wrong type.
if ( $field->is_administrative() || ! in_array( $field->get_input_type(), $field_types_to_check ) ) {
continue;
}
// Skipping fields which don't have a value.
$value = $field->get_value_export( $entry );
if ( empty( $value ) ) {
continue;
}
// Regular expressions for URLs and email addresses
$url_regex = '~(https?|ftp):\/\/\S+|www\.\S+~';
$email_regex = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/';
// If the value contains a URL or email address, mark submission as spam.
if ( preg_match( $url_regex, $value ) || preg_match( $email_regex, $value ) ) {
return true;
}
}
return false;
}
@jodzeee
Copy link
Author

jodzeee commented Jan 6, 2025

Sends form submissions to spam if they contain URLs or an email address.

I was using this plugin, but it alerts the spammer they aren't allowed. This snippet will send the entries to spam so they don't know they're being caught.
https://wordpress.org/plugins/reject-urls-and-emails-in-textarea/

Started with this:
https://docs.gravityforms.com/gform_entry_is_spam/#h-check-field-values-for-urls

And asked chatGPT to help add email addresses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment