In this article i will explain you how to get and update the information of wordpress user or woo-commerce customer.
What is metadata
meta data is data about the data.In wordpress we have seprate table for each type of data like user_meta table for users table.
Here are some more example
1.usersmeta table
2. postsmeta table
3. commentsmeta table
The main information of each type of data is store in main table and additional information of that data will be stored on meta table.Meta table as key and value for each record of main table.
WordPress Metadata API
WordPress has written lot of function to get/update data and other functionaltiy.wordpress has written some function to get and update the information from the meta tables.
And you can also add new field or meta to any table using those wordpress readymade functions.
Add/Delete Metadata
add_metadata()
delete_metadata()
Get/Update Metadata
get_metadata()
update_metadata()
You can read more about those function at codex
WordPress add User Meta(user field) – add addition feild to wordpres user profile
If you want to add new field to wordpress user profile.then you can use wordpress function
Let’s suppose we want to add mobile number to wordpress user profile
WordPress function
<?php add_user_meta( $user_id, $meta_key, $meta_value, $unique ); ?>
Here $user_id is the id of wordpress user and meta_key is the key or name of that field.and value will be the same value of field.
$user_d = 3 // this is user id of wordpress user
<?php add_user_meta( $user_id, 'mobile', 000000000 ); ?>
abobe code will run for specific user and if you want to add new mobile field to every wordpress user profile.then you can add the following function to your
function.php (function.php of current activated theme)
if ( ! function_exists( 'add_profile_field' ) ) {
function add_profile_field( $contactmethods ) {
$contactmethods[‘mobile’] = __(‘Mobile Number’);
return $contactmethods;
}
add_filter(‘user_contactmethods’,’add_profile_field’, 11, 1);
}
Here ‘user_contactmethods’ is method which is used to add or remove the profile information of wordpress user.
If you want to read about add_filter then read my article on the same category.
WordPress get User Meta – Get addition information of wordpress user
If you want to get those mobile number on any page or api then you can use following code.
<?php get_user_meta( $user_id, 'mobile' ); ?>
Here the $user_id is the current wordpress user id.You can get any other field value by same function
for example
<?php add_user_meta( $user_id, 'email' ); ?>
following code will show all the information of wordpress user.
<?php add_user_meta( $user_id); ?>
WordPress update User Meta – update the information of wordpress user custom field ( additional field
Use following code to update meta information of wordpress user
<?php update_user_meta( $user_id, 'mobile',11111111); ?>
WordPress Delete user meta – Delete the custom feield of wordpress user profile.
If you want to remove record from wordpress user meta table.You can remove those record using this function.
<?php delete_user_meta( $user_id, 'mobile' ) ?>