How to get author details in WordPress
In WordPress, we can use the get_the_author_meta(string $field)
function to retrieve the author’s details stored in the database object. get_the_author_meta()
function return the author’s requested information in string format if available, otherwise, it returns an empty string.
Note: By default,
get_the_author_meta()
function returns the current page/post author’s details. If you want any specific author details, you have to pass the author/user id e.g.get_the_author_meta( string $field, int $user_id)
.
1. Get author ID
get_the_author_meta('ID')
returns the ID of the author.
$author_id = get_the_author_meta('ID');
echo 'ID: '.$author_id;
Output
ID: 7
2. Get author user name/login id
get_the_author_meta('user_login')
returns the username/login name of the author.
$author_username = get_the_author_meta('user_login');
echo 'Username: '.$author_username;
Output
Username: lucy
3. Get author first name
get_the_author_meta('first_name')
returns the first name of the author.
$author_first_name = get_the_author_meta('first_name');
echo 'First name: '.$author_first_name;
Output
First name: Lucifer
4. Get author last name
get_the_author_meta('last_name')
returns the last name of the author.
$author_last_name = get_the_author_meta('last_name');
echo 'Last name: '.$author_last_name;
Output
Last name: Series
5. Get author display name
get_the_author_meta('display_name')
returns the display name of the author.
$author_display_name = get_the_author_meta('display_name');
echo 'Display name: '.$author_display_name;
Output
Display name: Lucifer Series
6. Get author nickname
get_the_author_meta('nickname')
returns the nickname of the author.
$author_nickname = get_the_author_meta('nickname');
echo 'Nick name: '.$author_nickname;
Output
Nick name: Dr. Champion
7. Get author description
get_the_author_meta('description')
returns the description/bio of the author.
$author_description = get_the_author_meta('description');
echo 'Description: '.$author_description;
Output
Description: Hi!, I'm Lucifer, a Full-Stack Web Application developer. I love Java and other open-source technology, currently working at one of the top MNC in India.
8. Get author level
get_the_author_meta('user_level')
returns the level/role of the author.
$author_level = get_the_author_meta('user_level');
echo 'Level: '.$author_level;
Output
Level: 1
9. Get author URL
get_the_author_meta('user_url')
returns the URL/website address of the author.
$author_url = get_the_author_meta('user_url');
echo 'URL: '.$author_url;
Output
URL: https://websparrow.org
10. Get author profile image
get_the_author_meta()
can not directly return the author’s profile image. We have to use the get_avatar()
function to retrieve the author’s profile image. get_avatar()
function returns the <img>
tag for an author.
$author_profile_image = get_avatar(get_the_author_meta('ID'), 96);
echo 'Profile photo: '.$author_profile_image;