By
05/15/2016   Comments Off on Displaying user info

View all snippets

Get current user’s info:

<?php
    $current_user = wp_get_current_user(); // set variable
    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User email: ' . $current_user->user_email . '<br />';
    echo 'User first name: ' . $current_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    echo 'User ID: ' . $current_user->ID . '<br />';
    echo 'User Gravatar: ' . get_avatar( $current_user->ID, 80 ); // the number is the size of the avatar
?>

Conditional statements for logged in users:

<?php if (is_user_logged_in()): // if logged in ?> ... <?php endif; ?> 

<?php if (! is_user_logged_in()): // if logged out ?> ... <?php endif; ?>

Create a link to the logged in user’s author page:

<a href="<?php $current_user = wp_get_current_user(); 
echo 'http://website.com/author/'.$current_user->user_login ?>">View Profile</a>

// User logout link

<a href="<?php echo wp_logout_url(); ?>">Log Out</a>

Change the author base from “author” to something else. In this example, I changed it to “profile”. Add this to your functions.php file:

// Author slug change

add_action('init', 'change_author_base');
function change_author_base() {
  global $wp_rewrite;
  $author_slug = 'profile'; // change slug name
  $wp_rewrite->author_base = $author_slug;
}