Displaying a phone number without a country prefix

Is it possible to display only the local number by omitting the country prefix along with the “+” sign?

Hi @rex658,

See this old topic:

Second last reply should be what you’re looking for!

Cheers,
Chris :victory_hand:

1 Like

Hi. This topic has not necessary solves. I need to fully remove country prefix.

add_filter('hivepress/v1/fields/phone/display_value', function($value, $field) {
    // Remove the "+" prefix if it exists
    if (strpos($value, '+') === 0) {
        $value = ltrim($value, '+');
    }
    return $value;
}, 1000, 2);

Try this snippet, @rex658.

Cheers,
Chris :victory_hand:

2 Likes

Yes, this snippet is working, but I need to remove full country prefix, like +972 for Israel. I need to use only local number as 0511222333. What can I do for this?

I made a change for remove prefix:

add_filter('hivepress/v1/fields/phone/display_value', function($value, $field) {
    // Remove the "+972" prefix if it exists
    if (strpos($value, '+972') === 0) {
        $value = ltrim($value, '+972');
    }
    return $value;
}, 1000, 2);

How I can adding a “0” to the beginning of the string?

Sorry, I was able to do it on my own :star_struck:

add_filter('hivepress/v1/fields/phone/display_value', function($value, $field) {
    if (strpos($value, '+972') === 0) {
        $value = '0' . substr($value, 4);
    } elseif (strpos($value, '+') === 0) {
        $value = ltrim($value, '+');
    }

    if (strlen($value) === 11 && strpos($value, '05') === 0) {
        $value = substr($value, 0, 5) . ' ' . substr($value, 5);
    }

    return $value;
}, 1000, 2);
1 Like

Glad to hear you managed to figure it out, @rex658!

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