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
    • WP Unicorn

    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

  • Greg
    • The Incredible Code Injector

    Hello @vi@Vinod Dalvi

    Thanks for helping today!

    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.

    2nd part: Correct :slight_smile:

    Cheers

  • Vinod Dalvi
    • WP Unicorn

    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
    • The Incredible Code Injector

    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
    • WordPress Hacker

    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

    • Greg
      • The Incredible Code Injector

      I have gone back to my site but my new users are still showing the username on the site. On single listing pages. The code was in my function.php child theme file.

      I then attempted to add to mu-plugins but it breaks my site.

      Help please!!

      Cheers

      PS. Just looked and in the admin backend I can change the users display name. When I choose their full name and check front end, it shows full first and last name but not the first name and last initial as I wanted originally. So it appears the code isn't working :disappointed:

      • Ash
        • WordPress Hacker

        Hello Greg

        I have just tested the following code again and worked fine in my test site:

        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
        				      )
        			);
        		}
        	}
        }
        
        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
        }

        It was working before, right? Can you remember since then if you made any more changes related to this? And which theme you are using?

        Cheers
        Ash

  • Ash
    • WordPress Hacker

    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

    • Superhero 19
      • Deleted User

      Hello Ash,

      After using your code on my Wordpress 4.9.7 website I thought initially that everything is fine, it was showing First Name & Last Name First Initial , but then I noticed that my other website functionalities are affect like /profile , clients are unable to change their profile, when click on profile its just refreshing the website while it adds /profile on link, but the most important thing is that I cannot reverse the effect. I have removed the code from child them function.php file with no effect , its still showing showing First Name & Last Name First Initial , and clients are unable to edit their profile anymore. I have downloaded the entire website and searched for the code, its not present. It seems like your code altered the entire database, if I restore an old database backup the problem is gone but the backup its too old, I Need to fix the problem on the actual DB. I have also tried to run the code again by replacing 'hide' with 'show' but it has no effect ! The big problem is that no one is able now to edit their profile, I need solution to resolve that, if not, PLEASE HELP to restore everything as before !

  • Dan
    • WPMU DEV Initiate

    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.

  • Kasia Swiderska
    • Support nomad

    Hello Dan,

    Please create separate thread for your issue as this one was resolved and we don't want to annoy author of it with new answers that are not related to his question.
    Also we will be able to check this issue on your site - in other member thread it is not possible.

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.