Get the Current username
Write the following code in your functions.php file to get current user name with short code.
- Login to your WordPress admin Dashboard.
- Now from the left sidebar go to Appearance -> Theme Editor.
- From the right side panel, select functions.php file and write the below code snippet at the end of the file just before the closing php tag(?>)
//Get Logged-in username with shortcode
function custom_shortcode_func() {
ob_start();
$current_user = wp_get_current_user();
echo ‘Welcome ‘ . $current_user->display_name;
$output = ob_get_clean();
return $output; }
add_shortcode(‘current_user’, ‘custom_shortcode_func’);
Call Username on header
- Now with the help of Shortcode [current_user] you can display current username wherever you want on the website.
- As we want to display the current username on the right top of the header, let us call this shortcode from our header.php file.
- Please select header.php file from the right side panel of your Theme Editor page and write the below code snippet to display username on header.
<!– Display Logged-in Username –>
<?php if( is_user_logged_in() ) { ?>
<div id=”et_right_header_current_user”>
<?php echo do_shortcode( “[current_user]” ); ?>
</div>
<?php } ?>
Position the Name to right
- Give a unique identifier name as shown in the above picture.
- Now go to Theme customizer from Appearance -> Customizer from the left sidebar
- Now open Additional CSS tab</li
</ul- Now add the below code snippet to position username on right side of your header
/* Position the Name to right */
#et_right_header_current_user{
float:right; }
- Add more CSS styles based on your requirement of positioning username on your website.