Skip to main content
Login Join
Snippet · PHP

Display Current User Display Name

Shared by Utsav Tilava · August 22, 2026

4 views
1 upvote
Back to Snippets

This snippet creates a shortcode that displays the current user’s WordPress display name. It is useful for personalized greetings, dashboards, and member areas.

<?php
function display_current_user_name() {

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

    $user = wp_get_current_user();

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

add_shortcode( 'current_user_name', 'display_current_user_name' );
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