Need username to = first_name+last_name

I have a registration page created using the Ultimate membership plugin, I don't want people to have to enter a username for registration. I tried hiding the username field and using some code, changing the display name to first name & last name.

However, since WordPress requires a username by default, it uses the user's email address as the username instead. For example this account (https://****nark.com/members/++++++gmail-com/)

I am wanting to automatically make the username = first_name-last_name . rand()

We thought just making the username field somehow a hidden field on the registration form, and then perhaps with some Javascript we could automatically fill in the user_login field with the $_POST data for first_name & last_name with a random number at the end when they fill in the form.

  • Ash
    • WordPress Hacker

    Hello Howard

    Please try the following code:

    add_action( 'wp_head', function(){
    	?>
    	<style>
    		#ihc_reg_text_3609{display: none !important}
    	</style>
    	<script>
    		jQuery(function($){
    			$( 'input[name="first_name"], input[name="last_name"]' ).keyup(function() {
    				var fname = $('input[name="first_name"]').val(),
    					lname = $('input[name="last_name"]').val(),
    					username = fname + '-' + lname + Math.floor((Math.random() * 10000) + 1);
    
    				$('input[name="user_login"]').val(username);
    			});
    		});
    	</script>
    	<?php
    } );

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Ash
    • WordPress Hacker

    Hello Howard

    I have added the required code as mu-plugin:

    <?php
    
    add_action( 'wp_head', function(){
    	?>
    	<style>
    		input[name="user_login"]{display: none !important}
    	</style>
    	<script>
    		jQuery(function($){
    			$( 'input[name="first_name"], input[name="last_name"]' ).keyup(function() {
    				var fname = $('input[name="first_name"]').val(),
    					lname = $('input[name="last_name"]').val(),
    					username = fname + '-' + lname + Math.floor((Math.random() * 10000) + 1);
    
    				$('input[name="user_login"]').val(username);
    			});
    		});
    	</script>
    	<?php
    } );

    Would you please check and let me know now? Have a nice day!

    Cheers,
    Ash

  • Howard
    • New Recruit

    Hi Ash, the signup form is now visible, however after filling it out, I get an 'invalid username' message. Would you be able to personally test the form yourself to see if the changes are working as it would be a much more efficient way to solve this quickly?

    Thank You

  • Howard
    • New Recruit

    Also, the login form seems be having the same problem as the signup from previous previously was, it appears blank when the code is entered. As such, I have temporarily removed the code from the live site so that atleast the login form can work for other users who are testing the site. Would it be possible if we run the tests on the dev site before applying the changes to the live site?

    Thank you

  • Ash
    • WordPress Hacker

    Hello Howard

    Let's do this on a dev site. Would you please send me your dev site admin login and cPanel login?

    You can send me privately in the following format:
    1. Please go to https://premium.wpmudev.org/contact/#i-have-a-different-question
    2. Select "I have a different question" if not selected
    3. In subject line put "Attn-Ashok" (this will ensure that the email is assigned to me)
    4. Provide the wanted details.
    5. Add the thread link in the message so that I can track.

    I will take a look as soon as possible.

    Have a nice day!

    Cheers,
    Ash

  • Nithin
    • Support Wizard

    Hi Howard,

    I gave a closer look, and tried couple of workaround, but it doesn't seem to help. I'm bringing this into our Second Level Support(SLS) teams attention, so that they could give a closer look, and help get it fixed asap.

    We'll get back to you via the ticket asap once we have tested this extensively.

    Regards,
    Nithin

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Howard ,

    I've edited the mu-plugin at the dev site that you've created and commented out the user_register action that you had. If everything is handled by the form itself there's no need to re-do in within the WP registration hooks as well that's why I've commented it out, I'm not sure though if you had it there for a different purpose/form also so please adjust that if needed.

    I've added a wp_footer action instead that has a jquery snippet handling the form.

    function custom_code_for_register() {
        ?>
        <script>
            ( function ( $ ) {
                $( document ).ready( function () {
                $( '.ihc-form-create-edit input[name="user_login' ).attr( 'type', 'hidden' );
    
                  $( '.ihc-form-create-edit #ihc_submit_bttn' ).click( function() {
                    var first = $( '.ihc-form-create-edit input[name="first_name"]' ).val().toLowerCase(),
                        last  = $( '.ihc-form-create-edit input[name="last_name"]' ).val().toLowerCase(),
                        rand  = Math.floor((Math.random() * 10000) + 1),
                        uname = first + last + rand;
    
                    $( '.ihc-form-create-edit input[name="user_login"' ).val( uname );
                  } );
                } );
            } ( jQuery ) );
        </script>
        <style>
            .ihc-form-create-edit .iump-form-text:first-child {
                display: none !important;
            }
        </style>
        <?php
    }
    add_action( 'wp_footer', 'custom_code_for_register', 999 );

    I've registered myself a user using this form and this is the current result:

    Could you make a test as well for confirmation?

    Regards,
    Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.