Using Gravity Forms ‘gform_is_valid_url’ PHP filter

The gform_is_valid_url filter in Gravity Forms allows you to customize URL validation.

Usage

add_filter('gform_is_valid_url', 'custom_validation', 10, 2);

Parameters

  • $is_valid (bool): True if the URL is valid, false otherwise.
  • $url (string): The URL being validated.

More information

See Gravity Forms Docs: gform_is_valid_url

Examples

Basic Custom Validation

Custom validation that checks if the URL contains ‘example.com’.

function custom_validation($is_valid, $url) {
    if (strpos($url, 'example.com') !== false) {
        return true;
    } else {
        return false;
    }
}
add_filter('gform_is_valid_url', 'custom_validation', 10, 2);

Validate URL Based on a Specific Scheme

Custom validation that ensures URLs are HTTPS.

function validate_https_scheme($is_valid, $url) {
    return parse_url($url, PHP_URL_SCHEME) === 'https';
}
add_filter('gform_is_valid_url', 'validate_https_scheme', 10, 2);

Validate URL with Custom Allowed Domains

Custom validation that allows URLs only from specific domains.

function validate_custom_domains($is_valid, $url) {
    $allowed_domains = ['example1.com', 'example2.com'];
    $parsed_url = parse_url($url);
    return in_array($parsed_url['host'], $allowed_domains);
}
add_filter('gform_is_valid_url', 'validate_custom_domains', 10, 2);

Validate URL with a Custom Path

Custom validation that checks if the URL contains a specific path.

function validate_custom_path($is_valid, $url) {
    $required_path = '/blog';
    $parsed_url = parse_url($url);
    return strpos($parsed_url['path'], $required_path) !== false;
}
add_filter('gform_is_valid_url', 'validate_custom_path', 10, 2);

Validate URL with a Custom Query Parameter

Custom validation that checks if the URL contains a specific query parameter.

function validate_custom_query_parameter($is_valid, $url) {
    $required_parameter = 'utm_source';
    $parsed_url = parse_url($url);
    parse_str($parsed_url['query'], $query_params);
    return isset($query_params[$required_parameter]);
}
add_filter('gform_is_valid_url', 'validate_custom_query_parameter', 10, 2);