Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimwhite/d80a6983cc4b35afc009060b518f4705 to your computer and use it in GitHub Desktop.
Save kimwhite/d80a6983cc4b35afc009060b518f4705 to your computer and use it in GitHub Desktop.
Generate a username during checkout for user [Paid Memberships Pro]
<?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