How to Work With WordPress User Metadata

Work With WordPress User Metadata

It is one of the nicest APIs that WordPress offers is a WordPress user Metadata API. Because of this API, we get ideas about the posts, comments, users, terms, and a lot more that holds the information about the standard set of data information.

1. WordPress Metadata :
Metadata is taken care of with key/value sets. The key is the name of the metadata component. The value is the data that will show up in the metadata list on every individual post that the data is related. As we keep on working with the different WordPress user metadata APIs, you’re going to find that this definition remains constant regardless of which API is being investigated.

2. Working with User Metadata API :
The WordPress user metadata will be stored in the wp_usermeta database table, there is actually some data is stored already in the user metadata API. This happens because some of the data stored on the user profile screen.
In any case, the API will enable us to compose our very own data to the table. So with the majority of that state, we should feel free to investigate how to function with the capacities given by WordPress.
Through the majority of the examples given, we will be passing 1 for the main parameter to the API capacities since the primary client is dependably the site director. This is for the most part destined to be available in some random establishment.

3. Adding user meta :
Add metadata to a user’s record for this you can function add_user_meta. This is beneficial, if you somehow managed to take a shot at a plugin or a web application that is based on WordPress and you’re hoping to broaden what an individual can connect with their profile, at that point this is one approach to do it.

It could be something as basic as giving a client’s profile on a given interpersonal organization, or it could be something further developed where you might connect the client with information contained in another table, a variety of data, or something different.

Be that as it may, the API work acknowledges a discretionary fourth parameter on regardless of whether the value being embedded ought to be remarkable or not.

4. Non-unique values :
Here we will see at the coding part for adding user metadata.

add_filter(‘the_content', ‘add_user_meta');
/**
* Determines if the current post is the default ‘Hello Wordl' post and, if so,
* adds non-unique user meta data to the database.
*
* @param    string $content   The post content.
* @return   string $content   The post content.
*/
function add_user_meta($content) {
If (1===get_the_ID()){
add_user_meta( 1, ‘twitter_account', ‘https://twitter.com/username');
}
return $content;
}

We hook into the_content
We check to see if we are on the Hello World post
If yes than we add the user metadata
We return $content to WordPress.

With this code set up and with the Hello World post stacked in your program, revive the page a couple of times.

Unique values
Now when we know about the non-unique values, in the first function, in the second function we are going to see unique values.

add_filter(‘the_content', ‘unique_add_user_meta');
/**
* Determines if the current post is the default ‘Hello Wordl' post and, if so,
* adds unique user meta data to the database.
*
* @param    string $content   The post content.
* @return   string $content   The post content.
*/
function unique_add_user_meta($content) {
If (1===get_the_ID()){
add_user_meta( 1, ‘twitter_account', ‘https://twitter.com/username',true);
}
return $content;
}

Initially, give an interesting incentive to the meta value in the function call. Refresh the page a couple of times, and after that take a look at the database.

5. Updating User Meta :
Update user meta field dependent on user ID. Utilize the $prev_value parameter to separate between meta fields with a similar key and user ID. On the off chance that the meta field for the user does not exist, it will be included.
When working with this capacity, it contemplates this in two situations:

At the point when previous metadata has been included utilizing the add_user_meta work and there are various records with similar data.
At the point when no metadata has been included and we’re including another record and need it to be unique.
In the main case, it gives the $prev_value since you’re revealing to WordPress which incentive to target and to update.

As like add and update one can use delete and get function for obtaining WordPress user metadata.

6. Syntax :

Delete#
Delete metadata coordinating criteria from a client. You can coordinate dependent on the key, or key and value. Deleting dependent on key and value, will keep from evacuating copy metadata with a similar key. It additionally permits expelling all metadata coordinating key, if necessary.

Detete_user_meta(
int $user_id,
string $meta_key,
mixed $meta_value="
);

7. Retrive user meta :

With regards to recovering user metadata, we have the get_user_meta work. Now, it ought to be evident that the normal parameters will be the user ID and the meta key.
Keep in mind when we’re retrieving data, we just need the user ID and the meta key since that is the distinguishing data for a particular value.

Get#
get_user_meta(
int $user_id,
string $key ="
b001 $single=false
);

Summary!
For the time being, keep on exploring different avenues regarding the code that has been given in this blog. Keep in mind that it’s intended for demonstration purposes just and ought not to be kept running in a production environment.

All through this arrangement, we’re endeavoring to establish a framework for future WordPress developers to work from when they go ahead and deal with answers for their boss, their customers, or for their own tasks.

So, I’m anticipating proceeding with this arrangement. Keep in mind in case you’re simply beginning, you can look at my arrangement on the most proficient method, to begin with, WordPress, which centers around subjects explicitly for WordPress tenderfoots. Kindly don’t delay to leave any inquiries or remarks in the feedback or comment section.

This blog will help you to know with the working and how to work with the WordPress User Metadata.

Thank you for visiting SKT Themes a place for selecting top WordPress Themes from a list of several hundred templates. Happy Reading and Shopping.