Using Gravity Forms ‘gform_phone_formats’ PHP filter

The gform_phone_formats filter allows you to add new phone formats to the Phone field in Gravity Forms.

Usage

Add the following code to apply the filter to all forms:

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

To apply the filter only to a specific form, append the form ID to the filter name:

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

Parameters

  • $phone_formats (array): An array containing the default phone formats.
  • $form_id (integer): The ID of the current form.

More information

See Gravity Forms Docs: gform_phone_formats

Examples

Add a UK phone format for all forms

This example adds a UK phone format, including a validation regex. It doesn’t use an input mask and doesn’t display any instruction text if the field fails validation.

add_filter('gform_phone_formats', 'uk_phone_format');

function uk_phone_format($phone_formats) {
    $phone_formats['uk'] = array(
        'label'       => 'UK',
        'mask'        => false,
        'regex'       => '/^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$/',
        'instruction' => false,
    );

    return $phone_formats;
}

Add a Spanish phone format for all forms

This example adds a Spanish phone format, including a validation regex that accepts only Spanish numbers in the local format (no international prefix). It doesn’t use an input mask and doesn’t display any instruction text if the field fails validation.

add_filter('gform_phone_formats', 'es_phone_format');

function es_phone_format($phone_formats) {
    $phone_formats['es'] = array(
        'label'       => 'Spain',
        'mask'        => '999999999',
        'regex'       => '/^[9|6|7|8][0-9]{8}$/',
        'instruction' => 'Introduce los 9 dígitos sin guiones ni espacios.',
    );

    return $phone_formats;
}

Place the above code in the functions.php file of your active theme.