Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions gp-advanced-phone-field/gpapf-save-national-format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Gravity Perks // Advanced Phone Field // Save Phone Number In National Format
* https://gravitywiz.com/documentation/gravity-forms-advanced-phone-field/
*
* Save the phone number in the national format, rather than international.
*
* For example, rather than a US number saving as +15555551234, the number would be saved as (555) 555-1234.
*/
// Update "123" to your form ID and "4" to your Phone field ID.
add_action( 'gform_save_field_value_123_4', function( $value, $entry, $field, $form, $input_id ) {

if ( ! is_callable( 'gp_advanced_phone_field' ) || ! class_exists( '\libphonenumber\PhoneNumberUtil' ) || rgget( 'page' ) != 'gf_entries') {
return $value;
}

$proto = gp_advanced_phone_field()->get_phone_number_proto( $value );
$phone_number_util = \libphonenumber\PhoneNumberUtil::getInstance();
if ( ! $proto ) {
return $value;
}
try {
return $phone_number_util->format( $proto, \libphonenumber\PhoneNumberFormat::NATIONAL );
} catch ( Exception $e ) {
error_log( 'Error formatting phone number: ' . $e->getMessage() );
return $value;
}

}, 10, 5 );
Loading