The user meta, including the custom ones, are saved in the wp_usermeta
table. You can get all user properties and meta keys using get_userdata( $userid ), an alias of get_user_by( 'id' )
, which returns a WP_User object or false on failure (the user with that ID does not exist).
Furthermore, you can determine if a certain meta key or property is set for a certain user using __isset($key)
. You can access a meta key or property using __get($key)
;
For example, you can make a shortcode to display user properties and/or meta keys with
add_shortcode('get-user-property', 'get_user_property');
function get_user_property( $atts ) {
$atts = shortcode_atts( array(
'user' => get_current_user_id(),
'key' => 'display_name',
), $atts, 'get-user-property');
$ud = get_userdata($atts['user']);
return $ud ?
($ud->__isset($atts['key']) ?
$ud->__get($atts['key']) :
(isset($ud->{$atts['key']}) ?
$ud->{$atts['key']} :
'[not set]'
)
) :
'[user does not exist]';
}
Now you can get any user property/meta-key using [get-user-property user="%id%" key="%field_name%"]
anywhere you have shortcodes enabled. If the prop is not set for a user, it will return [not set]
, or [user does not exist]
if you got the wrong ID.
If run without any arguments, it will return the display_name of the current user.