Skip to content

Commit 8d6db0d

Browse files
authored
gpapf-custom-format.php: Added new example snippet for saving field value with a custom phone number format.
1 parent 2715518 commit 8d6db0d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Advanced Phone Field // Custom Phone Number Format
4+
* https://gravitywiz.com/documentation/gravity-forms-advanced-phone-field/
5+
*
6+
* Create a custom number format and use this format for the default format saved to the field value. This example will
7+
* uses the international number (e.g. `+1 757-123-4567`) as a base and formats it like so: `+1 757.123.4567`.
8+
*/
9+
// Update "123" to your form ID and "4" to your Phone field ID.
10+
add_action( 'gform_save_field_value_983_1', function( $value, $entry, $field, $form, $input_id ) {
11+
12+
if ( ! is_callable( 'gp_advanced_phone_field' ) || ! class_exists( '\libphonenumber\PhoneNumberUtil' ) ) {
13+
return $value;
14+
}
15+
16+
$proto = gp_advanced_phone_field()->get_phone_number_proto( $value );
17+
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
18+
$international = $phone_number_util->format( $proto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL );
19+
20+
$international_parts = preg_split( '/[\s-]+/', $international );
21+
$dialing_code = array_shift( $international_parts );
22+
$formatted = sprintf( '%s %s', $dialing_code, implode( '.', $international_parts ) );
23+
24+
return $formatted;
25+
}, 10, 5 );

0 commit comments

Comments
 (0)