Skip to content

Instantly share code, notes, and snippets.

@vaughanm
Created December 2, 2015 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaughanm/e86802c785b706d121a4 to your computer and use it in GitHub Desktop.
Save vaughanm/e86802c785b706d121a4 to your computer and use it in GitHub Desktop.
adding user shortcode to display any user data
// Add Shortcode
function user_shortcode( $atts ) {
$a = shortcode_atts(
array(
'user_id' => get_current_user_id(),
'field' => '',
), $atts );
$user = get_user_by( 'id', $a['user_id'] );
$output = !empty($user->$a['field']) ? $user->$a['field'] : '';
return $output;
}
add_shortcode( 'user', 'user_shortcode' );
@vaughanm
Copy link
Author

vaughanm commented Dec 2, 2015

Now you can display any user data via shortcode.

[user field="user_login" user_id="1"]

will display the user login for user id 1. if user_id is omitted, defaults to current_user.

[user field="first_name" user_id="1"]

will display the first name for user_id 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment