Skip to content

Instantly share code, notes, and snippets.

@codenathan
Last active July 22, 2021 11:30
Show Gist options
  • Save codenathan/50b392d7bc731e839253ebcab3826e0a to your computer and use it in GitHub Desktop.
Save codenathan/50b392d7bc731e839253ebcab3826e0a to your computer and use it in GitHub Desktop.
Worpress Contact Form 7 - Textarea Validation - Your Message
add_filter( 'wpcf7_validate_textarea', 'no_urls_allowed', 10, 3 );
add_filter( 'wpcf7_validate_textarea*', 'no_urls_allowed', 10, 3 );
function no_urls_allowed( $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );
$type = $tag->type;
$name = $tag->name;
$value = isset( $_POST[$name] )
? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) )
: '';
// If this is meant to be a URL field, do nothing
if ( 'url' == $tag->basetype || stristr($name, 'url') ) {
return $result;
}
// Check for URLs
$value = $_POST[$name];
$not_allowed = array( 'http://', 'https://', 'www.', '[url', '<a ', ' seo ' );
foreach ( $not_allowed as $na ) {
if ( stristr( $value, $na ) ) {
$result->invalidate( $tag, 'URLs are not allowed' );
return $result;
}
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment