Show First Name & Last Name First Initial with Period Only

Hello Team!

Wanted to show 'James D.' instead of 'James Doe' - I want to prevent users to choose what to display, hide nickname option and 'display name as' and force first name & last name initial with period.

Cheers

  • Vinod Dalvi

    Hi Greg,

    I hope you are well today and thank you for your question.

    Wanted to show 'James D.' instead of 'James Doe'

    force first name & last name initial with period.

    Could you please tell me where do you want to show it so that i can help you to achieve it?

    I want to prevent users to choose what to display, hide nickname option and 'display name as'

    Do you want to remove it from the user profile page in the admin area as shown in the attached screenshot?

    Please advise.

    Kind Regards,
    Vinod Dalvi

  • Vinod Dalvi

    Hi Greg,

    1st part: I want to show that on the front end under the users front display profile. I want the public to see the forced first name & last name initial with period while the backend I can still see their complete first and last name.

    You can display it in the front end by adding the following code in the appropriate template file where you want to display it.

    <?php
    
    	if( is_user_logged_in() ){
    		$current_user = wp_get_current_user();
    		if( $current_user->user_firstname ){
    	    	echo 'User Name : ' . $current_user->user_firstname.' ';
    		}
    	    if( $current_user->user_lastname ){
    	    	echo substr($current_user->user_lastname, 0, 1).'. <br />';
    		}
    	}else{
    		echo "User is not logged in.";
    	}
    
    ?>

    If you are not sure where to add the provided code then please share me the page URL where you want to display it so that i can help you to add it in appropriate place.

    I want to prevent users to choose what to display, hide nickname option and 'display name as'

    You can achieve this by using the attached small plugin.

    Best Regards,
    Vinod Dalvi

  • Greg

    Hello @Vinod Dalvi

    Thanks for your help mate!

    In terms of where it shows, go to the homepage, and click on the only business listed and it will take you to a single listing. You'll see the username on the right sidebar. If you do add the code can you create a new .php file on my child if possible? Want it to stay in case the theme gets updated.

    You guys are amazing as usual :slight_smile:

    Cheers

  • Ash

    Hello @Greg

    I hope you are well today.

    Please try the following code:

    add_action( 'admin_init', 'fix_display_name' );
    function fix_display_name() {
    	$users = get_users();
    	foreach( $users as $user ){
    		if( $user->first_name != '' && $user->last_name != '' ){
    			$display_name = $user->first_name . ' ' . substr( $user->last_name, 0, 1 ) . '.';
    			wp_update_user(
    				array(
    				      'ID' => $user->ID,
    				      'display_name' => $display_name
    				      )
    			);
    		}
    	}
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Please note that, this code will only work when an user has first name and last name set in the profile field.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Ash

    Hello @Greg

    I hope you are well today.

    Please try the following code:

    add_action( 'admin_head', 'hide_profile_field' );
    function hide_profile_field() {
        ?>
        <script type="text/javascript">
        jQuery(function($) {
            $('.user-nickname-wrap, .user-display-name-wrap').hide();
        });
        </script>
        <?php
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Dan

    I added the code you outlined above, but it slows down my website extremely.

    add_action( 'admin_init', 'fix_display_name' );
    function fix_display_name() {
    	$users = get_users();
    	foreach( $users as $user ){
    		if( $user->first_name != '' && $user->last_name != '' ){
    			$display_name = ucfirst($user->first_name ). ' ' . ucfirst(substr( $user->last_name, 0, 1 ) ). '.';
    			wp_update_user(
    				array(
    				      'ID' => $user->ID,
    				      'display_name' => $display_name
    				      )
    			);
    		}
    	}
    }

    I believe it may be because of "get_users", and since we have 150 users, I'm assuming this just keeps running and running.

    What we need, is for this code to do an initial big run, to change all existing users display names to be First Name, Last Initial with a period (e.g. John D.).

    Once that's done, we need the display name to only update when 1) A user updates their profile or 2) A new user registers.

    How can we tweak the code to achieve this? We also need to make sure it does not slow down the site as it previously did.

    Any help would be greatly appreciated. Thank you.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.