Site icon PluginBuddy

LearnPress – Remove The Username From The Registration Form

Remove the username from the LearnPress registration form. It will automatically generate a username by email, like WooCommerce. It is inspired by the WooCommerce function.

Create a file wp-content/plugins/lp-user.php. Then add the following code and activate the plugin from wp-admin > plugins > installed plugins.

OR

You can add the code in the selected theme functions.php file.

<?php 
/**
 * Plugin Name: LP Remove Username
 * Description: LearnPress remove username
 * Version: 1.0.0
 */

add_action('learn-press/after-form-register-fields', 'learnpress_remove_username');
function  learnpress_remove_username(){
    ob_start();?>
    <script>var secondLi = document.querySelector('.lp-form-fields li:nth-child(2)');
    secondLi.style.display = 'none';</script>
    <?php echo ob_get_clean();
}

The learnpress_remove_username function is helping to hide the username field. You can hide it by theme style.css also.

Next, create username generating function. We want to generate a username from an email address. It will work recursively. It will execute again and again to get the unique username. The function name is learnpress_create_new_customer_username.

function learnpress_create_new_customer_username( $email, $new_user_args = array(), $suffix = '' ) {

    $email_parts    = explode( '@', $email );
    $email_username = $email_parts[0];

    // Exclude common prefixes.
    if ( in_array(
        $email_username,
        array(
            'sales',
            'hello',
            'mail',
            'contact',
            'info',
        ),
        true
    ) ) {
        // Get the domain part.
        $email_username = $email_parts[1];
    }

    $username_parts[] = sanitize_user( $email_username, true );
	

	$username = strtolower( implode( '.', $username_parts ) );

	if ( $suffix ) {
		$username .= $suffix;
	}

	if ( username_exists( $username ) ) {
		// Generate something unique to append to the username in case of a conflict with another user.
		$suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 );
		return learnpress_create_new_customer_username( $email, $new_user_args, $suffix );
	}

	return apply_filters( 'learnpress_new_customer_username', $username, $email, $new_user_args, $suffix );
}

Finally, add username name to username field during checkout process.

add_action('init', 'learnpress_init_username', 1);
function learnpress_init_username() {  
   if( empty($_REQUEST['reg_email']) ) return;  
   $_REQUEST['reg_username'] = learnpress_create_new_customer_username( $_REQUEST['reg_email'], [], '_lp' );
}

Complete LearnPress Remove Username Plugin File

/**
 * Plugin Name: LP Remove Username
 * Description: LearnPress remove username
 * Version: 1.0.0
 */
add_action('learn-press/after-form-register-fields', 'learnpress_remove_username');
function  learnpress_remove_username(){
    ob_start();?>
    <script>var secondLi = document.querySelector('.lp-form-fields li:nth-child(2)');
    secondLi.style.display = 'none';</script>
    <?php echo ob_get_clean();
}

add_action('init', 'learnpress_init_username', 1);
function learnpress_init_username() {  
   if( empty($_REQUEST['reg_email']) ) return;  
   $_REQUEST['reg_username'] = learnpress_create_new_customer_username( $_REQUEST['reg_email'], [], '_lp' );
}



/**
 * Create a unique username for a new customer.
 *
 * @param string $email New customer email address.
 * @param array  $new_user_args Array of new user args, maybe including first and last names.
 * @param string $suffix Append string to username to make it unique.
 * @return string Generated username.
 */
function learnpress_create_new_customer_username( $email, $new_user_args = array(), $suffix = '' ) {

    $email_parts    = explode( '@', $email );
    $email_username = $email_parts[0];

    // Exclude common prefixes.
    if ( in_array(
        $email_username,
        array(
            'sales',
            'hello',
            'mail',
            'contact',
            'info',
        ),
        true
    ) ) {
        // Get the domain part.
        $email_username = $email_parts[1];
    }

    $username_parts[] = sanitize_user( $email_username, true );
	

	$username = strtolower( implode( '.', $username_parts ) );

	if ( $suffix ) {
		$username .= $suffix;
	}

	if ( username_exists( $username ) ) {
		// Generate something unique to append to the username in case of a conflict with another user.
		$suffix = '-' . zeroise( wp_rand( 0, 9999 ), 4 );
		return learnpress_create_new_customer_username( $email, $new_user_args, $suffix );
	}

	return apply_filters( 'learnpress_new_customer_username', $username, $email, $new_user_args, $suffix );
}

Please feel free to PM to get support. https://pluginbuddy.com/contact-us/

Exit mobile version