# Apply function to all phone fields

**URL:** <https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732>\
**Category:** Get Help\
**Tags:** custom\
**Created:** [January 23, 2020, 9:23am UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732 "2020-01-23T09:23:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5aba34f10f2bc3.1](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@user5aba34f10f2bc3.1](https://community.gravityforms.com/u/user5aba34f10f2bc3.1)\
**Post date:** [January 23, 2020, 9:23am UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/1 "2020-01-23T09:23:25Z")

</div>

Hallo,

We have a function that should check phone number and to add country code at the begining. It is working good but we need to find a way so we can apply it to all phone fields on different forms.  
On that way we won’t need to found out phone field ID for each form and to call function for each form separately. Here you can see our function. Thanks!

```
add_action( 'gform_pre_submission_10', 'code_phone' );
function code_phone( $form ) {
	$org_value = $_POST['input_4'];
	if (substr($org_value, 0, 1) === '0') {
		$final_value = '+49' . substr($org_value, 1);
	}
	$_POST['input_4'] = $final_value;
}
```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [January 23, 2020, 1:57pm UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/2 "2020-01-23T13:57:33Z")

</div>

You can check the `$field['type']` in your code and apply it only to type == ‘phone’ fields. It would look something like this (untested):

```auto
add_action( 'gform_pre_submission_10', 'code_phone' );
function code_phone( $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}." );
			if ( substr( $org_value, 0, 1 ) === '0' ) {
				GFCommon::log_debug( __METHOD__. "(): Substring matches." );
				$final_value = '+49' . substr( $org_value, 1 );
				$_POST["{$field}"] = $final_value;
				GFCommon::log_debug( __METHOD__. '(): Altered POST' . print_r( $_POST, true ) );
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![uamv](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/uamv/32/1412_2.png) [@uamv](https://community.gravityforms.com/u/uamv)\
**Post date:** [January 23, 2020, 5:23pm UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/3 "2020-01-23T17:23:24Z")

</div>

You’d also need to strip the `_10` from the filter in order to apply this to all forms and not only form ID 10. Thus, the first line:

```auto
add_action( 'gform_pre_submission', 'code_phone' );

```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [January 23, 2020, 5:24pm UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/4 "2020-01-23T17:24:28Z")

</div>

Oh yeah, good point 🙂

---

<div class="post-metadata">

**Author:** ![user5aba34f10f2bc3.1](https://avatars.discourse-cdn.com/v4/letter/u/ba9def/32.png) [@user5aba34f10f2bc3.1](https://community.gravityforms.com/u/user5aba34f10f2bc3.1)\
**Post date:** [January 23, 2020, 5:42pm UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/5 "2020-01-23T17:42:13Z")

</div>

Thank you for your fast response. I just removed form ID and use it as it is. It worked perfect!

---

<div class="post-metadata">

**Author:** ![richardw8k](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/richardw8k/32/2354_2.png) [@richardw8k](https://community.gravityforms.com/u/richardw8k)\
**Post date:** [January 5, 2022, 8:54am UTC](https://community.gravityforms.com/t/apply-function-to-all-phone-fields/3732/6 "2022-01-05T08:54:32Z")

</div>

Closed on January 5, 2022, 8:54am UTC


