get_the_author_meta( string $field = '', int|false $user_id = false ): string
Retrieves the requested data of the author of the current post.
Contents
Description
Valid values for the $field
parameter include:
- admin_color
- aim
- comment_shortcuts
- description
- display_name
- first_name
- ID
- jabber
- last_name
- nickname
- plugins_last_view
- plugins_per_page
- rich_editing
- syntax_highlighting
- user_activation_key
- user_description
- user_email
- user_firstname
- user_lastname
- user_level
- user_login
- user_nicename
- user_pass
- user_registered
- user_status
- user_url
- yim
Parameters
-
$field
string Optional -
The user field to retrieve.
Default:
''
-
$user_id
int|false Optional -
User ID. Defaults to the current post author.
Default:
false
Return
string The author's field from the current author's DB object, otherwise an empty string.
More Information
If used within The Loop, the user ID need not be specified, it defaults to current post author. A user ID must be specified if used outside The Loop.
get_the_author_meta() returns the data for use programmatically in PHP. To just display it instead, use the_author_meta()
If the specified meta field does not exist for this user, an empty string is returned.
Plugins may add additional fields to the user profile, which in turn adds new key/value pairs to the wp_usermeta database table. This additional data can be retrieved by passing the field’s key to the function as the $field
parameter.
Source
File: wp-includes/author-template.php
.
View all references
function get_the_author_meta( $field = '', $user_id = false ) {
$original_user_id = $user_id;
if ( ! $user_id ) {
global $authordata;
$user_id = isset( $authordata->ID ) ? $authordata->ID : 0;
} else {
$authordata = get_userdata( $user_id );
}
if ( in_array( $field, array( 'login', 'pass', 'nicename', 'email', 'url', 'registered', 'activation_key', 'status' ), true ) ) {
$field = 'user_' . $field;
}
$value = isset( $authordata->$field ) ? $authordata->$field : '';
/**
* Filters the value of the requested user metadata.
*
* The filter name is dynamic and depends on the $field parameter of the function.
*
* @since 2.8.0
* @since 4.3.0 The `$original_user_id` parameter was added.
*
* @param string $value The value of the metadata.
* @param int $user_id The user ID for the value.
* @param int|false $original_user_id The original user ID, as passed to the function.
*/
return apply_filters( "get_the_author_{$field}", $value, $user_id, $original_user_id );
}
Hooks
-
apply_filters( "get_the_author_{$field}",
string $value ,int $user_id ,int|false $original_user_id ) -
Filters the value of the requested user metadata.
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Display the author bio (description) keeping the line break
Get the author ID Outside loop:
Get the author ID inside a loop :
bellow is a few examples of author value :
Using the wpautop() for description will also keep the line break (like in studio-jt’ comment) but will output cleaner html:
Get the author ID:
Show a User’s Display Name With Email Address Linked
Get the email address for user ID 25, and echo it using their display name as the anchor text.
Get A User’s Email Address
Get the email address for the author of the current post and store it in the
$user_email
variable for further use. (Remember, this function returns data, it doesn’t display it.)What are the author meta that I can use in “field”?
Could they be added to this page? Or a link to another page that lists them could be added perhaps?
Top ↑
Feedback
Just confirmed that it will get any user meta fields, including other ones created with
add_user_meta()
— By Bruno Cantuaria —