Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Forked from travislima/pmpro_format_my_phone.php
Last active December 16, 2024 09:51
Show Gist options
  • Save ipokkel/7cde94322a743a63f06e574ac27eaec0 to your computer and use it in GitHub Desktop.
Save ipokkel/7cde94322a743a63f06e574ac27eaec0 to your computer and use it in GitHub Desktop.
Format the Phone number of Paid Memberships Pro - Customized Example
<?php
/**
* This code gist illustrates a way to customize the format of your members phone number.
*
* In this example, we will change the PMPro phone number from
*
* (123)456-7890
* to 1234567890
*
* Adjust this code gist to suit your needs for phone number.
*
* We will be hooking into the pmpro_format_phone_filter. For more on this filter, please see: https://www.paidmembershipspro.com/hook/pmpro_format_phone/
*
* Add this code below to your PMPro Customizations Plugin
* - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function pmpro_format_my_phone( $phone ) {
$r = cleanPhone( $phone );
if ( strlen( $r ) == 11 ) {
// returns 0 123-456 78 90
$r = substr( $r, 0, 1 ) . ' ' . substr( $r, 1, 3 ) . '-' . substr( $r, 4, 3 ) . ' ' . substr( $r, 7, 2 ) . ' ' . substr( $r, 9, 2 );
} elseif ( strlen( $r ) == 10 ) {
// returns 123-456 78 90
$r = '' . substr( $r, 0, 3 ) . '-' . substr( $r, 3, 3 ) . ' ' . substr( $r, 6, 2 ) . ' ' . substr( $r, 8, 2 );
} elseif ( strlen( $r ) == 7 ) {
// returns 456 78 90
$r = substr( $r, 0, 3 ) . ' ' . substr( $r, 3, 2 ) . ' ' . substr( $r, 5, 2 );
}
/**
* Filter to do more or less cleaning of phone numbers.
*
* @since 1.8.4.4
*
* @param string $r The formatted phone number.
* @param string $phone The original phone number.
*/
return $r;
}
add_filter( 'pmpro_format_phone', 'pmpro_format_my_phone' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment