WordPress allows users to choose how their names appear on posts, pages, and comments. Sometimes, you may want to change an author’s public display name programmatically instead of asking the user to update it from the WordPress profile settings.
WordPress provides the wp_update_user() function to update user information, including the display name. You can use this function to change an author’s display name while keeping their username and login credentials unchanged.
In this tutorial, you will learn how to update an author’s display name using PHP and how to modify the displayed author name without changing the user’s saved profile information.
1. Understand the Difference Between Display Name and Username
Before changing an author name, it is important to understand the difference between the WordPress username and display name.
- Username: The account login name used to sign in to WordPress. It is not normally changed through the standard user profile update process.
- Display name: The public name shown on posts, comments, and other parts of the website where the author’s name is displayed.
- First and last name: Optional profile fields that can be used to build the author’s public display name.
Changing the display name does not change the user’s login name, password, or account ID.
2. Get WordPress User Information Using get_userdata()
First, you need the user ID of the author whose display name you want to change. WordPress provides the get_userdata() function to retrieve information about a user.
$user_id = 1;
$user = get_userdata( $user_id );
if ( $user ) {
echo esc_html( $user->user_login );
echo '<br>';
echo esc_html( $user->display_name );
}
Replace 1 with the actual WordPress user ID. The function returns a user object when the user exists, or false if the user cannot be found.
You can learn more about this function in the official WordPress get_userdata() documentation.
3. Change the Author Display Name Using wp_update_user()
The recommended way to permanently change a user’s display name is to use the WordPress wp_update_user() function.
The function accepts an array containing the user ID and the fields you want to update. For example, the following code changes the display name of user ID 1:
$user_id = 1;
$user_data = array(
'ID' => $user_id,
'display_name' => 'This Is Author',
);
$result = wp_update_user( $user_data );
if ( ! is_wp_error( $result ) ) {
echo 'Author display name updated successfully.';
} else {
echo esc_html( $result->get_error_message() );
}
After the update succeeds, WordPress saves the new display name in the user’s profile. It will be used wherever the website displays that user’s public name through the standard author functions.
For more information about updating user details, refer to the official wp_update_user() documentation.
4. Change the Display Name of the Current Logged-In User
If you want to update the display name of the currently logged-in user, you can retrieve the current user ID using get_current_user_id().
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$user_data = array(
'ID' => $user_id,
'display_name' => 'New Author Name',
);
$result = wp_update_user( $user_data );
if ( ! is_wp_error( $result ) ) {
echo 'Display name updated successfully.';
}
}
This example updates the profile of the logged-in user. If you use this code in a public-facing form or custom plugin, make sure the user is authorized to perform the update and validate the submitted name before saving it.
5. Change the Displayed Author Name Without Updating the User Profile
Sometimes, you may want to change how an author’s name appears on the front end without permanently changing the display name stored in the WordPress user profile.
For this purpose, you can use the get_the_author filter. It modifies the author name returned by the corresponding template function.
function geekerhub_modify_author_display_name( $display_name ) {
// Change the displayed author name.
$display_name = 'This Is Author';
return $display_name;
}
add_filter(
'get_the_author',
'geekerhub_modify_author_display_name'
);
This approach changes the value returned by get_the_author() when the filter runs. It does not update the user’s saved profile or change the login name.
If you want to apply the change only to a particular author, you can check the current author’s user ID before returning the modified name.
function geekerhub_modify_specific_author_name( $display_name ) {
$author_id = get_the_author_meta( 'ID' );
if ( 1 === (int) $author_id ) {
return 'This Is Author';
}
return $display_name;
}
add_filter(
'get_the_author',
'geekerhub_modify_specific_author_name'
);
Replace 1 with the user ID you want to target.
6. Which Method Should You Use?
Choose the method based on what you want to achieve:
- Update the saved profile name: Use
wp_update_user(). This permanently changes the user’s display name. - Change the name shown on the front end: Use the
get_the_authorfilter. This changes the value returned by the author template function without updating the profile. - Retrieve user details: Use
get_userdata()when you need to access the user’s existing information.
If you are adding this functionality to a custom WordPress plugin, see our guide on creating a custom plugin in WordPress.
Important Notes
- Use the correct user ID: Always confirm that the user exists before updating their profile.
- Check for errors: The
wp_update_user()function can return aWP_Errorobject if the update fails. - Check permissions: Do not allow visitors to change another user’s display name without proper authorization.
- Do not change the login name: Updating
display_namedoes not change the username used for authentication. - Filter scope: The
get_the_authorfilter affects output that uses the corresponding author function. It does not automatically change every custom author display implemented by themes or plugins. - Use a child theme or plugin: Add custom PHP to a child theme’s
functions.phpfile or a custom plugin rather than editing WordPress core files.
Conclusion
You can change an author’s display name in WordPress programmatically using the built-in wp_update_user() function. This updates the saved user profile while leaving the username and login credentials unchanged.
If you only want to modify the author name displayed on the front end, use the get_the_author filter instead. Choose the method that matches your requirement and make sure the code has the appropriate user permissions.