Hello,
This post follows on from previous posts on the subject:
→ Format and Display Phone Numbers (Revisited) - General - HivePress Community,
→ Phone field format - General - HivePress Community.
For these customizations, you will need :
→ How to add custom code snippets - HivePress Help Center,
→ Code Snippets – WordPress plugin | WordPress.org.
Here’s how to customize your phone number :
Display 1
add_filter(
'hivepress/v1/fields/phone/display_value',
function($value){
$listing = hivepress()->request->get_context('listing');
if(!$listing){
return $value;
}
// remove the "+" sign from the beginning of the phone number
$value = ltrim($value, '+');
// display phone number
return preg_replace('~(\d{1}){0,7}(\d{2})[^\d]{0,7}(\d{2})[^\d]{0,7}(\d{2})[^\d]{0,7}(\d{2})~', '0$1.$2.$3.$4.$5.$6', $value);
},
1000
);
Display 2 :
add_filter(
'hivepress/v1/fields/phone/display_value',
function($value){
$listing = hivepress()->request->get_context('listing');
if(!$listing){
return $value;
}
// display phone number
return preg_replace('~(\d{2}){0,7}(\d{1})[^\d]{0,7}(\d{2})[^\d]{0,7}(\d{2})[^\d]{0,7}(\d{2})[^\d]{0,7}(\d{2})~', '($1) 0$2.$3.$4.$5.$6', $value);
},
1000
);
Display 3 :
add_filter(
'hivepress/v1/fields/phone/display_value',
function($value){
$listing = hivepress()->request->get_context('listing');
if(!$listing){
return $value;
}
// display phone number
return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '+1 ($1) $2-$3', $value);
},
1000
);
For other formats, please modify the following items :
Element 1
(\d{2}) → change the number to suit your needs.
Element 2
“$1 $2 $3 $4 … “ → And add the signs you want : “.”, “-”, “()”, …
__
I’d like to take this opportunity to reopen the question of the “call button”, which is a very interesting function :
→ Add a phone field for social links - Feature Requests - HivePress Community,
→ I want to display a call now button that use the attribute phone-num - Development - HivePress Community,
→ I want to display a call now button that use attribute phone_num - Development - HivePress Community.
While waiting for this function to be added, you can transform the phone attribute into a button that will open the phone application automatically with the following HTML code :
<a class="button button--primary hp-button hp-button--wide" href="tel:%value%" target="_blank" rel="nofollow">%icon% %value%</a>
To avoid opening a new page, you can delete : target=“_blank”
I hope this helps
Good luck in the development of your websites