Forked from andrewlimaza/generate-username-from-email-pmpro.php
Created
February 28, 2024 21:35
-
-
Save kimwhite/d80a6983cc4b35afc009060b518f4705 to your computer and use it in GitHub Desktop.
Generate a username during checkout for user [Paid Memberships Pro]
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
<?php | |
/** | |
* Make user name email address the same | |
* Hide your 'username' field using custom CSS. | |
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_generate_username_at_checkout() { | |
//check for level as well to make sure we're on checkout page | |
if (empty($_REQUEST['level'])) { | |
return; | |
} | |
if (!empty($_REQUEST['bemail'])) { | |
$_REQUEST['username'] = my_pmpro_generate_username_from_email($_REQUEST['bemail']); | |
} | |
if (!empty($_POST['bemail'])) { | |
$_POST['username'] = my_pmpro_generate_username_from_email($_POST['bemail']); | |
} | |
if (!empty($_GET['bemail'])) { | |
$_GET['username'] = my_pmpro_generate_username_from_email($_GET['bemail']); | |
} | |
} | |
add_action('init', 'my_pmpro_generate_username_at_checkout'); | |
/** | |
* Hide the username field on checkout. | |
*/ | |
function my_pmpro_unset_required_username_checkout( $pmpro_required_user_fields ) { | |
unset( $pmpro_required_user_fields['username'] ); | |
return $pmpro_required_user_fields; | |
} | |
add_filter('pmpro_required_user_fields', 'my_pmpro_unset_required_username_checkout', 10, 2); | |
/** | |
* Generate a username from a user's email address. | |
* Helper function. | |
*/ | |
function my_pmpro_generate_username_from_email($email) { | |
return $email; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment