How do I get user meta in WordPress?

How do I get user meta in WordPress?

You can get WP User Meta Data in 3 different ways in WordPress using PHP. These functions are,

  • get_userdata() native function
  • get_user_meta() native function or
  • by using the WP User Object if your are in an Object Orientated situation

In this article I’ll show you the PHP functions WordPress gives for accessing, editing and creating User Meta data.

Table of Contents:

What is User Meta in WordPress?

WordPress websites have a MYSQL database and within here the data for each user is stored in a table and can be referred to as the User Object so to speak. Name, user ID, email address etc etc are standard ones but the flexibility and extensibility of WordPress allows you to add customized ‘extra data’ to this data base as suits your particular website. This ‘extra data’ is what is known in WordPress as Meta Data. So User Meta Data is some piece of data (that is accessible from the User Object in the database) which is required for your website to do what it needs to do.

This piece of data is stored not directly in the User Table but instead if stored in another related table which can be referenced along with the User Table in order to retrieve, update, create or delete various pieces of User Meta Data as required.

PROTIP: WordPress stored the User Meta Data in a separate table for ease of use and so the user (which is often times not a developer) can add User Meta without having to edit the User Table directly. If you did edit it directly you could have the new piece of data inside the same User Meta table as all the other info. WordPress is made to be used by various plugins and non-developers and if you had to manually add the column to the User Meta table this would complicate development slightly. This has PROS and CONS but it is how WordPress is structured.

How to retrieve User Meta Data

You could do this in two ways depending on if you have the User Object already accessible in your code or if you have the User ID (as an integer).

PROTIP: If the User Meta you are looking up exists BUT is empty the results of the below WordPress functions may behave unexpectedly.

If you have the (integer) User ID:

Get all User Meta Data for a User:

This can be useful if you don’t know the exact name of the User Meta Data you are trying to access because you can get all of them print them out and then select the one which seems most appropriate. It can also be used if you actually want to access multiple User Meta Data for some reason and hence could get them all in once line of code.

To use this you will only need the User ID.

$allUserMetaData = get_userdata(13);
// print all the data there is to the screen
echo '<pre>';
print_r($allUserMetaData);
echo '</pre>';
// then you can peruse the results and look for the specific Meta Data (aka key) you want and once found you can access it as below.
echo $allUserMetaData->MetaKeyNameReplaceMe;

Get one specific User Meta Data for a User:

In order to use this you will first need to know the name of the Meta Data you want to access (if you don’t know what it is exactly see below) and decide if you want the info returned as a single value or as an array.

The original function in WordPress looks like this.

get_user_meta( int $user_id, string $key = '', bool $single = false )

But in order to use it specifically see below and replace the $key and the $single value to suit your own needs.

// get the User Meta as an array is one option
$exampleArray = get_user_meta( 13, 'MetaKeyNameReplaceMe', false );
$MetaValue = $exampleArray['MetaKeyNameReplaceMe'][0]; // this will be the actual value you want to use

// or you could just get the single value by changing the $single value to true
$MetaValue = get_user_meta( 13, 'MetaKeyNameReplaceMe', true );

If you have the User Object:

Sometimes, if you already have the WP User Object accessible in your application code, you may just want to use one of its methods to do something similar to the above. For example, you could also grab the same User Meta Data by doing something like this…

Tip: Remember to replace the key name with your own

// you need access to WP User Object to use this 
echo $userObject->get('MetaKeyNameReplaceMe');

What if you don’t know the exact name of the User Meta Data in order to use the above methods?

It’s possible that you are in need of a User Meta Value but you don’t exactly know what its Key Name is. In this case you can look for it in either the database or using a PHP.

Using the Database:

In PHPMYADMIN (if you have access to this on the server cPanel) find the ‘wpprefix_usermeta’ table and click into it. You will see something like the below image.

You can look in the ‘meta key’ column for the appropriate key to use for your situation.

look in the ‘meta key’ column for the appropriate key to use for your situation

Using PHP:

See the above section on printing out all the User Meta Data for a particular user above.

How to edit an existing User Meta Data:

WordPress has yet another function to achieve this. It’s very simple.

The original function looks like this.

update_user_meta( int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = '' );

But in your application you can do something like this.

$updateUserMeta = update_user_meta( 13, 'MetaKeyNameReplaceMe', 'newValue', '' );
// see the note below on the empty string value at the end.

NOTE: Previous value to check before updating (the 3rd parameter in the function). If specified, only update existing metadata entries with this value. Otherwise, update all entries.

How to create an existing User Meta Data:

You can do this in two different ways. The above method for updating a User Meta Data can also be used to create a new one (if it doesn’t already exist) or you can use this specific method which WordPress has.

Learn WordPress customization throughly with an online course at Udemy

If you are having difficulty understanding this tutorial and then I suggest you start from scratch with WordPress in general. The best way to learn is with a better all around general understanding of WordPress. Check out some of the courses on Udemy related to WordPress. They cover developer level courses and more non-dev CMS editor type tutorials as well.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *