Custom Fields: Access User Meta
The WordPress Custom Fields plugin stores the user values upon registration and profile update to that WordPress user’s meta fields in the WordPress usermeta table.
These user meta values stored by the Custom Fields plugin can be accessed just lke WordPress itself and many other plugins access user meta. Here are a few things you need to know in order to do this…
Things to Know
1. The WordPress get_user_meta function
WordPress has a built-in get_user_meta function which can easily access any user meta value by user ID and the meta key. First parameter is the ID of the user and the second parameter is the meta key from which the value will be taken. Here is the function:
<?php get_user_meta($user_id, $key, $single); ?>
The function can be executed anywhere. Inside your theme or even inside a plugin.
2. Getting the user ID value
It depends what the situation is and where you are using the get_user_meta function. You may already have the user ID in a variable or you received it from another plugin.
Getting the user ID of the current logged in user is easy. WordPress has a global PHP variable named $user_ID (note the uppercase ID) which holds the ID of the current user logged in. Here is an example:
<?php
global $user_ID;
echo 'Your WordPress user ID is: ' . $user_ID;
?>
Be sure to first define the global variable as shown above and then it will be available to you to use in your code.
3. Meta keys of the Custom Fields plugin
In order to use the get_user_meta function of WordPress mentioned above, you’ll need to know what meta key to use. This one is easy because the plugin gives it to you.
When you go to the Custom Fields section of the plugin you will see a table with all your current custom fields. The second column holds the meta key which you can use.
You will notice that each meta key is prefixed by “wpcf”. The plugin always has this prefixed to each meta key. One of the main reasons for this – apart from identification purposes – is that it prevents conflict with other meta keys from WordPress and other plugins.
4. Get value by number
Some values for custom fields such as country, select drop down, radio buttons, checkboxes, etc. will give you a number when you use the get_user_meta function.
The plugin has a built-in function named “get_field_value_by_number” that you can use which will automatically convert these numbers to the appropriate values for you.
<?php
//global user ID of current logged in user
global $user_ID;
//get the country ID from the usermeta table
$country_id = get_user_meta($user_ID, 'wpcfcountry', true);
if (class_exists('wpFields')) {
$wpFields = new wpFields();
//get the country value with the built-in "get_field_value_by_number" function
$country = $wpFields -> get_field_value_by_number("wpcfcountry", $country_id);
//output a string to the user
echo "You are located in " . $country;
}
?>
You can use the “get_field_value_by_number” function for any of the custom fields that return a number rather than the actual value you were expecting. Just remember to initialize the “wpFields” class as shown in the example above and then execute the “get_field_value_by_number” function/method with the custom field name/meta key.
Here is the code: http://tribulant.pastebin.com/6WMcHTCU
A Real Life Example
The 3 things mentioned above are the only 3 things you need to know in order to access and use user meta values stored by the Custom Fields plugin. The creativity is up to you and you can do with the user meta values what you need to do.
Lets say we created 4 custom fields with the Custom Fields plugin and put them on our WordPress registration for users to fill in when they register. Say these fields are:
- First Name
- Last Name
- Birth Date
- Country
Users go and register and fill in these fields so they are saved to the users’ user meta in the WordPress user meta table.
You may then have a custom WordPress page template which you send your users to where they can view all their details. But the page needs to display different details depending on the user that access the page and the details that are relevant to that specific user only. This page template may contain code like this:
<?php
//global WordPress variable with the current user ID
global $user_ID;
//check if the user is logged in and has a user ID
if ($user_ID) {
//get the user meta values by $user_ID and the meta keys from the plugin
if ($first_name = get_user_meta($user_ID, "wpcffirst_name", true)
&& $last_name = get_user_meta($user_ID, "wpcflast_name", true)
&& $birth_date = get_user_meta($user_ID, "wpcfbirth_date", true)
&& $country = get_user_meta($user_ID, "wpcfcountry", true)) {
//output a user legible string with the variables containing the meta values
echo 'Hello ' . $first_name . ' ' . $last_name . '! You were born on ' . $birth_date . ' and you live in ' . $country . '.';
}
} else {
//no user ID is available so the user is not logged in
echo 'You are not logged in';
}
?>
The code above is just given as a sample for reference. It will be useful in understanding how the user meta values are obtained and used in a real life situation.
Here is some more code to get you started: http://tribulant.pastebin.com/R04RCZBi
Earn Money by Referring People
Refer customers to us with your affiliate link and earn commission on sales from your link.