Skip to content

Instantly share code, notes, and snippets.

@travislima
Last active April 8, 2021 18:35
Show Gist options
  • Save travislima/98a45fab0bccc13b44ac9c1522b7dd00 to your computer and use it in GitHub Desktop.
Save travislima/98a45fab0bccc13b44ac9c1522b7dd00 to your computer and use it in GitHub Desktop.
Format the Phone number of Paid Memberships Pro
<?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 ) {
$r = substr( $r, 0, 1 ) . '' . substr( $r, 1, 3 ) . '' . substr( $r, 4, 3 ) . '' . substr( $r, 7, 4 );
} elseif ( strlen( $r ) == 10 ) {
$r = '' . substr( $r, 0, 3 ) . '' . substr( $r, 3, 3 ) . '' . substr( $r, 6, 4 );
} elseif ( strlen( $r ) == 7 ) {
$r = substr( $r, 0, 3 ) . '' . substr( $r, 3, 4 );
}
/**
* 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' );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "How to format the member phone number field using a filter." at Paid Memberships Pro here: https://www.paidmembershipspro.com/how-to-format-the-member-phone-number-field-using-a-filter/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment