Forked from greathmaster/pmpro-format-phone-number.php
Last active
October 7, 2021 15:07
-
-
Save kimwhite/29b1e00c6791acc8949e547d44e304c4 to your computer and use it in GitHub Desktop.
Actively formats phone number into XXX-XXX-XXXX format
This file contains hidden or 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
<?php // do not copy this line. | |
/** | |
* This recipe will Actively formats phone number into XXX-XXX-XXXX format | |
* on the PMPro chekcout page. if not using the billing phone, change line 18 # to be the id of your custom field | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function format_phone_number() | |
{ | |
//Adapted from: http://stackoverflow.com/questions/26412623/jquery-or-javascript-to-autoformat-phone-mask | |
echo | |
"<script> | |
<!-- | |
jQuery('#bphone').keyup(function(ev) { | |
var key = ev.which; | |
if (key < 48 || key > 57 || key != 45) { | |
ev.preventDefault(); | |
} | |
if (this.value.length > 12) { | |
this.value = this.value.slice(0, -1); | |
return; | |
} | |
this.value = this.value.replace(/^(\d{3})(\d)/, '$1-$2').replace(/^(\d{3}-\d{3})(\d)/, '$1-$2'); | |
}); | |
--> | |
</script>"; | |
} | |
add_action("pmpro_checkout_after_form", "format_phone_number"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment