Removing punctuation from phone numbers before sending data to Zapier

Using Zapier to send submissions to our booking software, but running into an issue with phone number formatting, like in this thread: Remove parentheses, space, and dash from phone numbers [RESOLVED]

Basically, our booking software only allows for numbers in International format (+19999999999), and will reject the submission if it includes dashes, parentheses, etc. So I am looking for a way to strip punctuation from phone number data before sending.

I modified the code from the thread mentioned above, but it doesn’t seem to be working. Here’s what I have in my functions.php:

add_filter( 'gform_zapier_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;
}

Make sure you’re using an actual Phone field (Advanced Fields > Phone) to collect the number and not something like a normal single line text field as that conditional will only validate for Phone fields.

Enabling logging and adding custom logging statements to your code is also always a good way to step through your code and determine where it may be failing: Custom Logging Statements - Gravity Forms Documentation

Thanks, Karl.

Yes, I am using the actual Phone field and have logging enabled. Everything there looks fine.

This isn’t an issue of validating the field data in Gravity Forms, it’s about modifying the data before it is sent to Zapier, because the dispatch software that the data is being sent to from Zapier doesn’t allow phone numbers with punctuation.

I know this is possible, because the user in the thread I linked to got it work for their system, and Zapier has a hook for this kind of thing (gform_zapier_field_value - Gravity Forms Documentation)

One thing I noticed in your code is that your parameters are in a different order:

From the documentation:
$value, $form_id, $field_id, $entry

From your code:
$value, $form, $entry, $field_id

As Karl mentioned, additional logging can help with that. For example, you could log each one of those parameters, to be sure they are what you expect them to be (in the correct order.) And you can log what you are returning before you return it to see if it looks the way you want.

Enable logging for Gravity Forms and then try this:

add_filter( 'gform_zapier_field_value', 'gform_truncate_phone_numbers', 10, 4 );
function gform_truncate_phone_numbers( $value, $form, $entry, $field_id ) {
	gf_zapier()->log_debug( __METHOD__ . '(): The Value => ' . print_r( $value, true ) );
	gf_zapier()->log_debug( __METHOD__ . '(): The Form => ' . print_r( $form, true ) );
	gf_zapier()->log_debug( __METHOD__ . '(): The Entry => ' . print_r( $entry, true ) );
	gf_zapier()->log_debug( __METHOD__ . '(): The Field ID => ' . print_r( $field_id, true ) );
	$field = GFAPI::get_field( $form, $field_id );
	gf_zapier()->log_debug( __METHOD__ . '(): The Field => ' . print_r( $field, true ) );
	if ( is_object( $field ) && ( $field->type == 'phone' ) ) {
		gf_zapier()->log_debug( __METHOD__ . '(): Matched if statement.' );
		$value = preg_replace('/[^0-9]/', '', $value);
	}
	gf_zapier()->log_debug( __METHOD__ . '(): Possibly Modified Value => ' . print_r( $value, true ) );
	return $value;
}

NOTE: I used your original parameter names/order, so you can see what is actually happening here. Give that a try and let us know if you still need assistance. We’ll need a link to the Gravity Forms Core log and Zapier log to check for those logging statements.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.