Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active November 3, 2025 15:38
Show Gist options
  • Save dwanjuki/5bad8dd0a97b276b0b3e62ed27c11c5e to your computer and use it in GitHub Desktop.
Save dwanjuki/5bad8dd0a97b276b0b3e62ed27c11c5e to your computer and use it in GitHub Desktop.
Custom usernames with numeric suffix if the username exists
<?php
/**
* Build usernames from first name initial and last name
* Add numeric suffix if username exists
*
* Add this code to your PMPro Customizations Plugin -
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_custom_username( $userdata ) {
if ( ! empty( $_REQUEST['first_name'] ) && ! empty( $_REQUEST['last_name'] ) ) {
$first_initial = substr( $_REQUEST['first_name'], 0, 1 );
$user_login = strtolower( $first_initial . $_REQUEST['last_name'] );
// Check if username exists and add/increment suffix
$new_user_login = $user_login;
do {
$user_login_exists = get_user_by( 'login', $new_user_login );
if($user_login_exists) {
$count = intval( filter_var( $new_user_login, FILTER_SANITIZE_NUMBER_INT ) );
$count++;
$new_user_login = $user_login . $count;
}
} while( $user_login_exists );
$userdata['user_login'] = $new_user_login;
}
return $userdata;
}
add_filter( 'pmpro_checkout_new_user_array', 'my_pmpro_custom_username', 10, 1 );
/**
* Make the username field not required 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment