You don’t need a plugin for that. I made this function. Of course, I could wrap it up and make a simple plugin out of it, but I’ve never been a fan of the plugin support hassle.
Add this to functions.php in your theme’s folder:
add_shortcode('gutt-u', 'gutt_get_user_attribute');
function gutt_get_user_attribute($atts = array()) {
global $current_user;
get_currentuserinfo();
extract( shortcode_atts( array(
'id' => $current_user->ID,
'login' => '',
'email' => '',
'slug' => '',
'field' => 'display_name'
), $atts) );
if ($login || $email || $slug) {
if ($login) $gatt_user = get_user_by('login', $login);
elseif ($email) $gatt_user = get_user_by('email', $email);
else $gatt_user = get_user_by('slug', $slug);
}
elseif ($id > 0) $gatt_user = get_user_by('id', $id);
else return;
if (in_array($field, array( 'ID', 'user_login', 'user_nicename', 'user_email', 'user_url', 'user_regitered', 'display_name')))
return $gatt_user->$field;
else return $gatt_user->__get($field);
}
How it works: Use [gutt-u] with id, login, email, slug and field parameters.
id – id of the user you want to retrieve data for; defaults to current user and function doesn’t return anything if id (or login, or email, or slug) is missing and current user is not logged in;
login – login (user_login) of the user you want to retrieve data for; overrides id, email and slug;
email – email of the user you want to retrieve data for; overrides id and slug;
slug – slug (user_nicename) of the user you want to retrieve data for; overrides id;
field – the field you want to retrieve from the user; defaults to display_name. It can be any default WP field or any user meta field as long as it exists, except user_password (I chose not to include it, but can be added to the fields array if needed).
Examples:
[gutt-u] => returns display_name of current user.
[gutt-u field=id] => returns ID of current user.
[gutt-u [email protected] field=some_user_meta_field] => returns the some_user_meta_field of the user registered with [email protected].
[gutt-u slug=acub field=user_registered] => returns the date of registration for the user with user_nicename = “acub”
I doubt sanitization is needed. From the few tests I’ve made, if no user is found or if the field doesn’t exist, it just returns nothing.
@jason, feel free to put my code to good use if you find it useful. Thank you for a robust plugin.