How to get the user’s first and last name in WordPress programmatically

If you’re working with users in WordPress, you may need to retrieve their first and last names for different purposes. Fortunately, WordPress makes it easy to retrieve user data with its built-in functions. In this tutorial, we’ll show you how to get a user’s first and last name using our own custom function in WordPress. We are also going to provide an alternative option with a WordPress plugin.

WordPress Users and their data in WordPress

Before we dive into the code, let’s review the basics of WordPress users. WordPress users are people who have an account on your WordPress website. There are several different types of users in WordPress based on their user roles, including administrators, editors, authors, contributors, and subscribers. Each user has a unique username and email address associated with their account, as well as additional user data such as their first and last name.

wordpress get user first name

To manage users in WordPress, navigate to the “Users” section of your WordPress dashboard. From here, you can view a list of all users on your website and manage their accounts as needed. If you want more advanced user management tools you can consider Users Insights.

bulk group user management

Retrieving user data in WordPress

To retrieve a user’s first and last name in WordPress, you’ll need to use one of the functions that WordPress provides for working with user data. The most commonly used functions by WordPress developers for retrieving user data in WordPress are the wp_get_current_user() and get_userdata(). These two functions combined can be used to retrieve user data such as their first name, last name, email address, and display name.

In this tutorial, we’ll be using the new WP_User() function to retrieve user data. This function allows you to create a new instance of the WP_User class, which provides access to the user’s data.

The code for our custom function

To retrieve a user’s first and last name in WordPress, we will create a new function called my_get_users_name(). This function takes an optional parameter called $user_id, which is the ID of the user whose first and last name we want to retrieve. If the $user_id parameter is not provided, the function will retrieve the current user’s first and last name.

 

Let’s break down the my_get_users_name() function to see how it works:

  1. First, we check if the $user_id parameter is provided. If it is, we retrieve the user’s data using the new WP_User() function. Otherwise, we retrieve the current user’s data using the wp_get_current_user() function.
  2. Next, we check if the user’s first name is available. If it is, we check if the user’s last name is also available. If it is, we return the user’s first and last name separated by a space. If the user’s last name is not available, we return the user’s first name.
  3. If the user’s first name is not available, we return the user’s display name.

Alternatively you can also use the wp_get_current_user() function like this:

It is important to notice that the wp_get_current_user() function needs a user id to be assigned. If the current user is not already set, it will be assigned to the logged-in individual. However, if there is no logged-in user, the current user will be set to 0, which is considered invalid and will not have any permissions.

Gain Valuable Insights From Your WordPress User Data

Displaying User Data

Now that we have created the function, let’s see how we can use it in our WordPress site.

To display the current user’s name, you can simply call the function like this:

This will output the current user’s name. If you want to display the name of a specific user, you can pass their user ID as a parameter like this:

This will output the name of the user with ID 123.

Another way to display user data on the front end is by creating a shortcode. A shortcode is a small piece of code that allows you to embed dynamic content into your WordPress posts and pages.

Here is an example of how to create a custom shortcode that will get the currently logged in user first name and last name and display them on the front end of a page:

To use this shortcode in your WordPress posts or pages, simply add the [my_users_name] shortcode to the content where you want to display the user’s name. If you want the shortcode to accept a user ID as an argument, then you would need to tweak that code to do so.  If you want the shortcode to accept a user ID as input, then you would have to modify that code accordingly.

How to get customer first and last name without code

If you want to get the first and last name of a customer without writing any code, you can use a plugin that adds this functionality to your WordPress site. One such plugin is the

Conclusion

In this tutorial, we have learned how to get a user’s first and last name in WordPress using PHP code. Using the my_get_users_name function that we’ve created, you can easily get the user’s full name or fallback to their display name if necessary.We have also explored different ways to display user data on our WordPress site, including using shortcodes and custom codes.

With this knowledge, you can now customize your WordPress site to display user information in a personalized way. You could use this code to display a user’s name in the header of your site, on their author page, or in a custom widget.

We hope this tutorial has been helpful, and that you’re now able to confidently get the first and last name of any user in WordPress with ease!