Domain URL DNS Checker For CF7

Domain URL DNS Checker For CF7

Contact Form 7 stands tall as one of the most popular WordPress plugins for creating versatile and efficient contact forms.

One often-overlooked aspect is validating user inputs, particularly when it comes to critical information like website URLs.

Fortunately, for WordPress users leveraging CF7, there exists a simple yet powerful method to bolster validation: Custom DNS checking.

  • Make sure the input is actually an URL in syntax.
  • Validate with DNS that it indeed is a working URL.

How to use:

When adding URL input in CF7, use:

[text* your-website]

Then copy this code into your child theme Functions.php:

add_filter( 'wpcf7_validate_text*', 'custom_website_validation_filter', 20, 2 );
function custom_website_validation_filter( $result, $tag ) {
    if ( $tag->name == 'your-website' ) {
        $domain = isset( $_POST['your-website'] ) ? trim( $_POST['your-website'] ) : '';
        // Check if input is a valid URL
        if ( ! filter_var($domain, FILTER_VALIDATE_URL) ) {
            $result->invalidate( $tag, "The input is not a valid URL." );
            return $result;
        }
        // Check DNS only if it's a valid URL
        $ip = gethostbyname($domain);
        if ( $ip == $domain ) { // If the domain resolves to itself, it means it couldn't resolve to an IP address
            $result->invalidate( $tag, "That is not a working URL!" );
        }
    }
    return $result;
}

The provided code snippet showcases a custom validation filter designed to scrutinize website URLs entered via Contact Form 7. At its core, the function custom_website_validation_filter intercepts form submissions, extracts the website URL input, and proceeds to verify its DNS records’ existence.

On your own page “Domain + Cloudflare“, we’ve leverage a variant of this

The following snippet allows us to 1. Check if the input is an URL 2. If it’s already taken.

Instead validating a response when the URL syntax is correct and responds to DNS, the following code validates the input when the URL syntax is correct, but doesn’t give a DNS response.

  • Make sure the input is actually an URL in syntax.
  • Validate with DNS that the URL is not taken.

‘Register new domain’ – Checker:

add_filter( 'wpcf7_validate_text*', 'custom_website_validation_filter', 20, 2 );
function custom_website_validation_filter( $result, $tag ) {
    if ( $tag->name == 'your-website' ) {
        $domain = isset( $_POST['your-website'] ) ? trim( $_POST['your-website'] ) : '';
        // Check if the input has a valid URL format
        if ( !empty($domain) && !preg_match('/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/', $domain) ) {
            $result->invalidate( $tag, "Please enter a valid URL! (example.fi)" );
            return $result; // Return early if the input is not a valid URL
        }
        // If the input is a valid URL format, then check DNS records
        $ip = gethostbyname($domain);
        if ( $ip != $domain ) {
            $result->invalidate( $tag, "This URL already exists!" );
        }
    }
    return $result;
}

Essentially the above snippet turns CF7 into an ‘available domain checker’!

Register a .fi domain with us, supercharged with Cloudflare.

fi