Created
October 10, 2019 13:05
-
-
Save fabienlege/b3c81769dc22605e97487adb658dd67b to your computer and use it in GitHub Desktop.
EN: Format french phone number with custom wordpress hook (filter) FR : Formater automatiquement un numéro de téléphone pour l'affichage uniforme sur las pages à l'aide d'un filtre wordpress personnalisé.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* _ _____ | |
* | | | ___| | |
* __ _ __ _ ___ _ __ ___ ___ | |__ ___ |___ \ | |
* / _` |/ _` |/ _ \ '_ \ / __/ _ \ | '_ \ / _ \ \ \ | |
* | (_| | (_| | __/ | | | (_| __/ | | | | (_) /\__/ / | |
* \__,_|\__, |\___|_| |_|\___\___| |_| |_|\___/\____/ | |
* __/ | | |
* |___/ | |
* | |
* >> https://agenceho5.com | |
*/ | |
/** | |
* Formate un numéro de téléphone | |
* @param string $phone_number : Phone number to convert | |
* @param string $mode (optionnal) : Format du numéro de téléphone à retourner ("display" ou "url") | |
* @return string Le numéro de téléphone formaté | |
*/ | |
public function phone_format($phone_number, $mode = "display"){ | |
if(preg_match("/^(\+33|0033|0)(\d).?(\d{2}).?(\d{2}).?(\d{2}).?(\d{2})$/",$phone_number, $parts )){ | |
unset($parts[0]); | |
unset($parts[1]); | |
switch($mode){ | |
case 'display': | |
$parts[2] = '0'.$parts[2]; | |
return implode(\apply_filters('phone_format_glue', ' '), $parts); | |
break; | |
case 'url': | |
return 'tel:0033'.implode('', $parts); | |
break; | |
default: | |
throw new \Exception("{$mode} : mode non reconnu", 1); | |
} | |
} | |
return $phone_number; | |
} | |
add_filter('phone_format', [$this, 'phone_format'], 10, 2); | |
// Pour l'affichage : | |
apply_filters('phone_format', $phone_number, 'url'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment