get_the_author_meta()
Retrieves the requested data of the current or requested user (post author).
No need to specify user ID if the function used inside The Loop, it returns the data of current post author, in this case. You need to specify the user ID only if the function is used outside The Loop.
To immediately display the data, use the_author_meta().
Some plugins can extend the user data (such data adds to wp_usermeta table specifying key & value). To get such new field value specify the name (key) of this new field.
Hooks from the function
Return
String
. User data, if it exists, otherwise an empty string.
Usage
get_the_author_meta( $field, $user_id );
- $field(string)
The user field to retrieve. Can be:
ID user_login | login user_pass | pass user_nicename | nicename user_email | email user_url | url user_registered | registered user_activation_key | activation_key user_status | status user_description | description (Биографические данные из профиля пользователя) user_firstname | first_name user_lastname | last_name user_level | $wpdb->prefix . 'user_level' roles display_name nickname rich_editing comment_shortcuts admin_color plugins_per_page plugins_last_view
Default: ''
- $user_id(int/false)
- User ID. If specify this parameter, then the function returns data of the specified user (author).
Default: false (current user/author data)
Examples
#1 Retrieve the user e-mail
Let's get the email of the current post author and place it in the $user_email variable to use it further in our code (remember that the function is retrieve the data. but not display it).
$user_email = get_the_author_meta( 'user_email' ); echo $user_email;
Note: if you retrieve the data on the separated single page, before the function call you need to call the_post() function, or you need to specify user ID in second parameter ($post->post_author).
#2 Retrieve user name and link to email.
Let's get e-mail of the user with ID = 25 and display his "display name":
<p>Write to the author's email: <a href="mailto:<?php echo get_the_author_meta('user_email', 25); ?>"> <?php the_author_meta('display_name', 25); ?> </a> </p>
Notes
- Global. WP_User. $authordata The current author's data.
Changelog
Since 2.8.0 | Introduced. |