Skip to main content
Login Join
Snippet · PHP

Display Current User Login Name

Shared by Utsav Tilava · August 22, 2026

3 views
1 upvote
Back to Snippets

This snippet displays the username of the currently logged-in WordPress user using a shortcode. If the visitor is not logged in, it displays a login message instead.

<?php
function display_current_username() {

    if ( ! is_user_logged_in() ) {
        return '<p>Please log in to view your username.</p>';
    }

    $user = wp_get_current_user();

    return '<p><strong>Username:</strong> ' . esc_html( $user->user_login ) . '</p>';
}

add_shortcode( 'current_username', 'display_current_username' );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments