Using Gravity Forms ‘gform_coupons_is_valid_code’ PHP filter

The gform_coupons_is_valid_code filter allows you to override the default alphanumeric validation check for coupon codes in Gravity Forms.

Table of contents

Usage

To use this filter, add the following code to your functions.php file:

add_filter('gform_coupons_is_valid_code', 'your_function_name', 10, 2);

Parameters

  • $is_alphanumeric (bool): Indicates if the coupon code is alphanumeric.
  • $field (Field Object): The field object representing the coupon code field.

More information

See Gravity Forms Docs: gform_coupons_is_valid_code

Examples

Mark all coupon codes as valid

This example marks all coupon codes as valid, regardless of whether they are alphanumeric or not.

add_filter('gform_coupons_is_valid_code', 'mark_coupon_valid', 10, 2);

function mark_coupon_valid($alphanumeric, $field) {
    return true;
}

Only allow numeric coupon codes

This example only allows coupon codes containing numbers.

add_filter('gform_coupons_is_valid_code', 'allow_only_numeric', 10, 2);

function allow_only_numeric($alphanumeric, $field) {
    return ctype_digit($field->value);
}

Allow coupon codes with specific prefixes

This example allows coupon codes with specific prefixes (e.g., ‘PRE-‘, ‘SPECIAL-‘).

add_filter('gform_coupons_is_valid_code', 'allow_specific_prefixes', 10, 2);

function allow_specific_prefixes($alphanumeric, $field) {
    $prefixes = array('PRE-', 'SPECIAL-');
    foreach ($prefixes as $prefix) {
        if (substr($field->value, 0, strlen($prefix)) === $prefix) {
            return true;
        }
    }
    return false;
}

Allow specific coupon codes

This example allows only specific coupon codes.

add_filter('gform_coupons_is_valid_code', 'allow_specific_codes', 10, 2);

function allow_specific_codes($alphanumeric, $field) {
    $allowed_codes = array('SAVE10', 'FREESHIP', 'EARLYBIRD');
    return in_array($field->value, $allowed_codes);
}

Custom alphanumeric validation

This example uses a custom alphanumeric validation that allows spaces and dashes.

add_filter('gform_coupons_is_valid_code', 'custom_alphanumeric_validation', 10, 2);

function custom_alphanumeric_validation($alphanumeric, $field) {
    return preg_match('/^[a-zA-Z0-9 -]+$/', $field->value);
}

Leave a Comment

Your email address will not be published. Required fields are marked *