Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Function to invalidate Contact Form 7 input, unless the fields is of type "url" or has "url" in its name, i.e. it is meant for URLs
add_filter( 'wpcf7_validate_text', 'no_urls_allowed', 10, 3 );
add_filter( 'wpcf7_validate_text*', 'no_urls_allowed', 10, 3 );
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;
}
@markandcurry

This comment has been minimized.

Copy link

@markandcurry markandcurry commented Aug 30, 2016

Great function! I'd been working with something similar on basic php forms but wasn't sure how to make it work for cf7. Thanks!

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