Remove parentheses, space, and dash from phone numbers

I have a client that uses a phone system that supports matching phone numbers that are stored in a custom field in ActiveCampaign with callers. The hitch is that it requires the numbers to be stored without formatting. Is there any way that I can remove the parentheses, space, and dash from phone numbers before processing the entry? I tried the following code, but it seems to reformat the data after the trigger is called:

add_action( ‘gform_pre_submission’, ‘gform_compress_phone_numbers’, 10, 1 );
function gform_compress_phone_numbers( form ) { GFCommon::log_debug( __METHOD__ . '(): ORIGINAL POST' . print_r( POST, true ) );
foreach ( $form[‘fields’] as $field ) {
if ( $field[‘type’] == ‘phone’ ) {
GFCommon::log_debug( METHOD . ‘(): I found a phone number field!’ . print_r( $field, true ) );
$field = 'input
’ . $field[‘id’];
$org_value = rgpost ( $field );
GFCommon::log_debug( METHOD . “(): Original value => {org_value}." ); _POST[”{$field}"] = preg_replace(’/[^0-9]/’, ‘’, org_value); GFCommon::log_debug( __METHOD__ . '(): Altered POST' . print_r( _POST, true ) );
}
}
}

Can you use a single line text field to collect the phone number with instructions to submit the number “without any parenthesis, spaces or dashes”?

yeah, sure, but I was hoping to create less hassle for the users. does this mean there’s no easy way to solve this programmatically?

If you want to send just digits to ActiveCampaign, and your regex works, you could use this filter instead (still store the data in the format you collected it in, but alter it when sending to ActiveCampaign):

The filter name for ActiveCampaign is gform_activecampaign_field_value.

If you need help converting your code over to use this filter instead, let us know.

Yippee! It works! And so clean too!

Here’s my final code:

add_filter( 'gform_activecampaign_field_value', 'gform_truncate_phone_numbers', 10, 4 );
function gform_truncate_phone_numbers( $value, $form, $entry, $field_id ) {
    $field = GFAPI::get_field( $form, $field_id );
 
    if ( is_object( $field ) && ( $field->type == 'phone' ) ) {
        $value = preg_replace('/[^0-9]/', '', $value);
    }
 
    return $value;
}

Thanks so much!

© 2008 - 2019. Gravity Forms is a project by Rocketgenius Inc.