Discover in this tutorial how to get and display all the metadata associated with a WordPress user using code.
All WordPress users have a number of metadata associated with them. These will depend on the theme and active plugins you have.
Therefore, you may be interested in knowing all this metadata. This information can allow you to access or list a series of users depending on the value they have in one of these metadata.
So in this tutorial you are going to learn how to get a list of all the metadata associated with a WordPress user.
This would be the result:
nickname => Nahuai
first_name => Nahuai
last_name => Badiola
description =>
admin_color => fresh
use_ssl => 0
show_admin_bar_front => true
- …
This is just an example of the first lines of a user.
Let’s see how to get it.
Steps to get all the metadata from a WordPress user
1. Create a shortcode
First you are going to encapsulate the metadata inside a shortcode so you can display the listing wherever you want.
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
With this code you create a shortcode that allows you to display a list with the metadata associated with a user where you add the shortcode [user_meta id=x]
.
The list displays key and value pairs:
$key
: metadata name.$value
: metadata value.
In addition, you add a parameter to the shortcode to manually add the user ID (id=x
).
There is only one step left to view the list.
2. Insert the shortcode where you want
The advantage of shortcodes is that you can insert them anywhere on the web.
You only have to type [user_meta id=x]
where you want it to be displayed, changing the x with the user ID. You can get it by checking the URL when you edit them or, if you want to make it easier, get the ID displayed directly in WordPress admin with this tutorial.
Once you have it, just add it as a parameter to the shortcode. For the user with ID=1 it would be like this: [user_meta id='1']
.
Bonus
If you don’t need something as elaborate as a shortcode to get and display the data, you can use the function print_r
. For example:
To see this and another 1097 code snippets of this website, login or subscribe here.
The only thing you would have to modify is the 1 for the user’s ID number.
Conclusions
Now you know how to get and display a list with the metadata associated with a WordPress user.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉