Skip Navigation

[Resolved] Regex Phone Numbers in a view

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Florian 4 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#1547741

I'm looking for a way to clear a phone field. Our users enter the telephone number as they see fit, sometimes with a hyphen, sometimes with brackets or other special characters. I would like to clean up everything for an output that is not a number or + sign, so that the output in a toolset view also works for a tel: link. Is there a useful way to do this with toolset?

#1548201

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

There are two ways to fix the issue actually, I do not know yet how your users submit the telephone number value. Are you using Toolset Form that allows users to submit the telephone number? If yes:
You can use the Toolset Form's API hook cred_before_save_data that will run before the form content gets saved to the database and clean the telephone number value submitted by the user with your unwanted characters.
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

OR

You can write custom shortcode where you grab the database value saved for the telephone number and clean the unwanted characters and return your desired string to display.
=> https://toolset.com/documentation/adding-custom-code/how-to-create-a-custom-shortcode/
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1549277

Thanks - Is there a practical example somewhere of how I can use it to create a toolset field that contains the purged value of another toolset field?

#1549293

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean you want to have two fields now where one field will contain the original value having those special characters and another field will contain the clean value (without those special characters)?

Are you using Toolset Forms where you are allowing user to submit the phone number or the phone number will be inputted from backend admin?

#1549529

Correct. We have a field with the telephone number in a visually appealing format that our employees enter directly themselves. This could look like this: +(23) 231 - 123 34-34 - to use it for a link, all the disturbing characters would have to be removed. We do not use Toolset Forms - by way of exception - in this context. 🙂

#1549629

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So the option for you is to add a shortcode that returns you the clean phone number string.

You should try to add the following shortcode to your theme's functions.php file ;
OR
Custom Code section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

function func_clean_phone_number($atts) {
    $phone = $atts["number"];
      
   $phone = preg_replace('/(\W*)/', '', $phone);
    return $phone;
}
add_shortcode("display-clean-number", "func_clean_phone_number");

You can add this shortcode as:

[display-clean-number  number='[types field="phone" output="raw"][/types]']

Where:
Replace "phone" with your Types field name.

Maybe the following link also may help you;
=. hidden link

#1551931

My issue is resolved now. Thank you! Great, that was a good help. The result is highly satisfactory.