How to display the username on a page

How can I display the username of the logged in person on a page? Best without a plugin

Michael

  • Nithin
    • Support Wizard

    Hi Michael,

    Hope you are doing great today. :slight_smile:

    I could see you are using Upfront theme, so it would be a bit different implementing this, please add the following code in your /mu-plugins:

    <?php
    function dev_display_username( $atts ){
    
    	global $current_user;
    
    	get_currentuserinfo();
    
    		if ( is_user_logged_in() ) {
     			return 'Username: ' . $current_user->user_login . "\n";
    
     		}
    		else {
    			return wp_loginout( $_SERVER['REQUEST_URI'], false );
    		}
    
    }
    add_shortcode( 'wpmu_uname', 'dev_display_username' );

    You'll have to create a PHP file, and copy the above code in it, and make sure to add the file in the /mu-plugins folder. Please check this link for more info about /mu-plugins:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    The code will create a shortcode, wpmu_uname, you can then add that shortcode anywhere you want, by using the following format: [wpmu_name].

    You can add the shortcode [wpmu_uname] in post/page content, and if you are trying to implement it using element, then please make sure to add the shortcode using the Widget element, and by selecting Text widget, as shown in the given screenshot.

    I tested this in my system, and it seems to work fine in Upfront, it displays the username of the logged in person.

    I hope this helps. Please let us know if you still need any further assistance. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.