Skip to content

Commit c67bbaf

Browse files
authored
gpapf-save-national-format.php: Added new snippet to save the phone number in the entry as the National format, rather than the default International format.
1 parent ea9b93d commit c67bbaf

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Gravity Perks // Advanced Phone Field // Save Phone Number In National Format
4+
* https://gravitywiz.com/documentation/gravity-forms-advanced-phone-field/
5+
*
6+
* Save the phone number in the national format, rather than international.
7+
*
8+
* For example, rather than a US number saving as +15555551234, the number would be saved as (555) 555-1234.
9+
*/
10+
// Update "123" to your form ID and "4" to your Phone field ID.
11+
add_action( 'gform_save_field_value_123_4', function( $value, $entry, $field, $form, $input_id ) {
12+
13+
if ( ! is_callable( 'gp_advanced_phone_field' ) || ! class_exists( '\libphonenumber\PhoneNumberUtil' ) || rgget( 'page' ) != 'gf_entries') {
14+
return $value;
15+
}
16+
17+
$proto = gp_advanced_phone_field()->get_phone_number_proto( $value );
18+
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
19+
if ( ! $proto ) {
20+
return $value;
21+
}
22+
try {
23+
return $phone_number_util->format( $proto, \libphonenumber\PhoneNumberFormat::NATIONAL );
24+
} catch ( Exception $e ) {
25+
error_log( 'Error formatting phone number: ' . $e->getMessage() );
26+
return $value;
27+
}
28+
29+
}, 10, 5 );

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy