gform_phone_formatsDescriptionUsageParametersExamples1. Add a UK phone format for all forms2. Add a Spanish phone format for all formsPlacementSinceSource Code
Description
Can be used to add new formats to those listed within the Phone field.
Usage
The following would apply to all forms.
1add_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)
1add_filter( 'gform_phone_formats_5', 'your_function_name', 10, 2 );
Parameters
$phone_formats array
The phone formats. The following shows the default declaration of this array.
1234567891011121314$phone_formats = array( 'standard' => array( 'label' => '(###) ###-####', 'mask' => '(999) 999-9999', 'regex' => '/^D?(d{3})D?D?(d{3})D?(d{4})$/', 'instruction' => '(###) ###-####', ), 'international' => array( 'label' => __( 'International', 'gravityforms' ), 'mask' => false, 'regex' => false, 'instruction' => false, ), );
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 or false 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 or false 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 above regex, (i.e. Phone format: (###) ###-####) or false if you don』t want to display the instruction.
$form_id integer
The ID of the current form.
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.
1234567891011add_filter( 'gform_phone_formats', 'uk_phone_format' );function uk_phone_format( $phone_formats ) { $phone_formats['uk'] = array( 'label' => 'UK', 'mask' => false, 'regex' => '/^(((+44s?d{4}|(?0d{4})?)s?d{3}s?d{3})|((+44s?d{3}|(?0d{3})?)s?d{3}s?d{4})|((+44s?d{2}|(?0d{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.
1234567891011add_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 should be placed in the functions.php file of your active theme.
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.