Get User Custom Field Values
- Author:Scott Reilly
- Version:1.0.1
- Last update:09 April 2008
- Compatibility:WP 2.0+, 2.1+, 2.2+, 2.3+ and 2.5+
- Comments:go here
- Download:[ zip ]
- Description:
Easily retrieve and control the display of any custom field values/meta data for the currently logged in user or any specified user.
-
Extended Description
This plugin provides functionality similar to the Get Custom Field Values plugin, but for user custom fields (which WordPress manages in a separate database table). This plugin does NOT help you in setting user custom field values, nor does it provide an interface to list or otherwise manage user custom fields.As such, this plugin can be viewed more as a utility for developers, until such time as user custom fields can be created and managed via a user-accessible interface (like how post custom fields are managed in the Write Post and Write Page admin pages).
Installation
- Download the file get-user-custom.zip and unzip it into your wp-content/plugins/ directory.
- Activate the plugin through the ‘Plugins’ admin menu in WordPress.
- (optional) Add filters for ‘the_user_meta’ to filter user custom field data (see the end of the plugin file for commented out samples you may wish to include). And/or add per-meta filters by hooking ‘the_user_meta_$field’
- Give a user a custom field with a value, or have user custom fields already defined. (This generally entails use of plugin(s) that utilize the user custom fields feature built into WordPress. By default, in a practical sense WordPress only sets the ‘first_name’, ‘last_name’, and ‘nickname’ user custom fields, so you could try using one of them, even if just for testing even though WordPress provides functions to get those particular fields.)
- Use the function
c2c_get_current_user_custom()if you wish to access user custom fields for the currently logged in user. Use the functionc2c_get_user_custom()to access user custom fields for a specified user. Prepend either of the two mentioned functions with ‘echo’ to display the contents of the custom field; or use the return value as an argument to another function.
-
Template Tags
Functions
<?php function c2c_get_current_user_custom( $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>This allows access to custom fields for the currently logged in user. If the current visitor is NOT logged in, then the `$none` value is returned.
<?php function c2c_get_user_custom( $user_id, $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>This allows access to custom fields for any user specified by the `$user_id` value.
Arguments
- $user_id
(only for c2c_get_user_custom()) The integer value of the user’s id. - $field
The name of the user custom field to display. - $before
(optional) The text to display before all field value(s). - $after
(optional) The text to display after all field value(s). - $none
(optional) The text to display in place of the field value should no field value exists; if defined as ” and no field value exists, then nothing (including no $before and $after) gets displayed. - $between
(optional) The text to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used. - $before_last
(optional) The text to display between the next-to-last and last items listed when multiple occurrences of the custom field exist; $between MUST be set to something other than ” for this to take effect.
Examples
<?php c2c_get_current_user_custom('first_name'); ?>
“Scott”<?php c2c_get_current_user_custom('favorite_colors', 'Favorite colors: '); /* Where the 'favorite_colors' user custom field has been defined with values ?>
“Favorite colors: blue, gray, green, black, red”<?php c2c_get_current_user_custom('favorite_colors', 'Favorite colors: <ul><li>', '</li></ul>', '', '</li><li>'); ?>“Favorite colors:
<ul><li>blue</li><li>gray</li><li>green</li><li>black</li><li>red</li></ul>"<?php echo c2c_get_user_custom(3, 'first_name', 'Hi, ', '. Welcome back.'); // where 3 is the id of the user we want ?>
“Hi, Scott. Welcome back.”
Frequently Asked Questions
-
Q: How do I assign users custom fields so that I can retrieve them using this plugin?
A: You’ll have to use another plugin to store custom fields for users, or directly use WordPress functions manually.
Release Log
- 09 Apr 2008 : v1.0.1 — Release notes.
Copyright & Disclaimer
Copyright © 2006-2008 by Scott Reilly (aka coffee2code)Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Acknowledgements
Thanks to all those who have contributed feedback and support!
