How can I get the current user email instead of user ID?

How can I get the current user email instead of user ID? I’m using the following shortcode:

add_shortcode( 'current_user_link', 'wppbc_current_user_link' );
function wppbc_current_user_link( $atts, $content ) {
   if ( is_user_logged_in() ) {
      $id = get_current_user_id();
      // make sure to change the URL to represent your setup.
      return "<a href="http://website.com/user-listing-page/user/{$id}">Your User Page</a>";
   }

   return ;
}

1 Answer
1

you can get all logged in users details by below function

<?php
    $current_user = wp_get_current_user();
    /**
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( !($current_user instanceof WP_User) )
     *     return;
     */
    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 />';
?>

Leave a Comment