WordPress – User Profile Info Shortcode

bbPress WordPress - General Functions Shortcodes PHP

Snippet Overview

WordPress – User Profile Info Shortcode

If you build any sites that allow WordPress users to log in on the front end and interact with the site you may want to create a page that displays their account information. Getting the information of a logged in user is pretty easy to do and can be wrapped in a shortcode. Below is a function that will generate a shortcode for viewing the profile information of a logged in user.

To use this function you must paste the following code into your WordPress theme’s functions.php file.

// User Profile Information Shortcode 

function profile_shortcode( $atts ) {
	
	if(is_user_logged_in()) { // check if user is logged in

		$current_user = wp_get_current_user();
		echo '<div class="profile">';
		echo get_avatar( $current_user -> ID, 70 ) . '<br />';  // Specify Avatar size (currently 70px)
		echo '<b>Username:</b> ' . $current_user->user_login . '<br />';
		echo '<b>Email:</b> ' . $current_user->user_email . '<br />';
		echo '<b>First:</b> ' . $current_user->user_firstname . '<br />';
		echo '<b>Last:</b> ' . $current_user->user_lastname . '<br />';
		echo '<b>Display Name:</b> ' . $current_user->display_name . '<br />';
		echo '<b>User ID:</b> ' . $current_user->ID . '<br />';
		echo '</div>';		
	}
    else {
		echo '<div class="profile-card-inactive">';
		echo '<h3>You must log in to view this information</h3>';
		echo '</div>'; 
	}
}

add_shortcode( 'profile', 'profile_shortcode' );
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply