This snippet displays the currently logged in user’s profile information on the front end using a shortcode [my_profile]. It shows username, display name, first name, last name, email, and user ID. If the user is not logged in, it shows a login link instead.
<?php
function display_current_user_profile() {
if ( ! is_user_logged_in() ) {
return '<p>Please <a href="' . wp_login_url() . '">log in</a> to view your profile.</p>';
}
$current_user = wp_get_current_user();
$output = '<div class="user-profile">';
$output .= '<h2>My Profile</h2>';
$output .= '<p><strong>Username:</strong> ' . esc_html( $current_user->user_login ) . '</p>';
$output .= '<p><strong>Display Name:</strong> ' . esc_html( $current_user->display_name ) . '</p>';
$output .= '<p><strong>First Name:</strong> ' . esc_html( $current_user->user_firstname ) . '</p>';
$output .= '<p><strong>Last Name:</strong> ' . esc_html( $current_user->user_lastname ) . '</p>';
$output .= '<p><strong>Email:</strong> ' . esc_html( $current_user->user_email ) . '</p>';
$output .= '<p><strong>User ID:</strong> ' . esc_html( $current_user->ID ) . '</p>';
$output .= '</div>';
return $output;
}
add_shortcode( 'my_profile', 'display_current_user_profile' );