Save Phone Number In National Format

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.

Code

Filename: gpapf-save-national-format.php

<?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 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.