Description
The gform_phone_formats
filter can be used to add new formats to those listed within the Phone field.
Usage
The following would apply to all forms.
add_filter( 'gform_phone_formats', 'your_function_name', 10, 2 );
To limit the scope of your function to a specific form, append the form id to the end of the hook name. (format: gform_phone_formats_FORMID)
add_filter( 'gform_phone_formats_5', 'your_function_name', 10, 2 );
Parameters
$phone_formats
The following shows the default declaration of the $phone_formats
array.
array(
'standard' => array(
'label' => '(###) ###-####',
'mask' => '(999) 999-9999',
'regex' => '/^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/',
'instruction' => '(###) ###-####',
'type' => 'standard',
),
'international' => array(
'label' => __( 'International', 'gravityforms' ),
'mask' => false,
'regex' => false,
'instruction' => false,
'type' => 'international',
),
)
Each phone format consists of four properties.
- label string
The label which will be displayed for the choice in the Phone Format setting, on the fields properties tab, in the form editor. - mask string | boolean
The input mask to be applied to the input when the form is displayed, orfalse
if an input mask should not be used. See the Input Mask article for some usage examples. - regex string | boolean
The regular expression which should be used when validating the field value on submission, orfalse
if validation of the value should not be performed. - instruction string | boolean
The text you want to display beneath the field input if the value fails the validation performed using the aboveregex
, (i.e.Phone format: (###) ###-####
) orfalse
if you donāt want to display the instruction.
Examples
1. Add a UK phone format for all forms
The following example adds a UK format, including validation regex. It does not enable an input mask and does not 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;
}
2. Add a Spanish phone format for all forms
The following example adds a Spanish format, including validation regex that will accept only Spanish numbers in local format (no international prefix). It does not enable an input mask and does not 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;
}
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Since
This filter was added in Gravity Forms 2.0-beta-2.2.
Source Code
This filter is located in GF_Field_Phone::get_phone_formats()
in includes/fields/class-gf-field-phone.php.