I’m trying to create shortcodes that will display both avatar and logged in user first name in a text widget. I have managed to create something that displays the avatar, but not the username.
My code is as follows:
<?php
// show user avatar if logged in
function colaborator_avatar($atts)
{
if (is_user_logged_in() && !is_feed()) {
return get_avatar(get_the_author_meta( 'user_email' ));
}
}
add_shortcode('colaborator_avatar', 'colaborator_avatar');
// show user avatar if logged in
/**/
function colaborator_nome($atts)
{
if (is_user_logged_in() && !is_feed()) {
return get_user_meta( $new_user->ID, 'first_name', true );
}
}
add_shortcode('colaborator_nome', 'colaborator_nome');
?>
Have in mind that this is not for an specific ID, this to show on screen which user is logged in.
By the way, is it possible to define the avatar’s size?
EDITED: Corrected a typo error in the code.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The issue here is that both the functions have the same name – colaborator_avatar().
Make sure that $new_user contains the current user. Else use get_current_user_id() like this:
get_user_meta( get_current_user_id(), 'first_name', true );
Show avatar and first name:
// show user avatar if logged in
function colaborator_avatar($atts)
{
if (is_user_logged_in() && !is_feed()) {
return get_avatar(get_the_author_meta( 'user_email' ));
}
}
add_shortcode('colaborator_avatar', 'colaborator_avatar');
// show username if logged in
function colaborator_nome($atts)
{
if (is_user_logged_in() && !is_feed()) {
return get_user_meta( $new_user->ID, 'first_name', true );
}
}
add_shortcode('colaborator_nome', 'colaborator_nome');
Or show Username:
function colaborator_nome($atts) {
if (is_user_logged_in() && !is_feed()) {
$current_user = wp_get_current_user();
echo $current_user->user_login;
}
}
add_shortcode('colaborator_nome', 'colaborator_nome');
Method 2
I searched for ‘wordpress get name of logged in user’ and the first result gave wp_get_current_user, which will show logged in user first name. Examples of how to use from that page:
$current_user = wp_get_current_user();
/*
* @example Safe usage: $current_user = wp_get_current_user();
* if ( ! ( $current_user instanceof WP_User ) ) {
* return;
* }
*/
printf( __( 'Username: %s', 'textdomain' ), esc_html( $current_user->user_login ) ) . '<br />';
printf( __( 'User email: %s', 'textdomain' ), esc_html( $current_user->user_email ) ) . '<br />';
printf( __( 'User first name: %s', 'textdomain' ), esc_html( $current_user->user_firstname ) ) . '<br />';
printf( __( 'User last name: %s', 'textdomain' ), esc_html( $current_user->user_lastname ) ) . '<br />';
printf( __( 'User display name: %s', 'textdomain' ), esc_html( $current_user->display_name ) ) . '<br />';
printf( __( 'User ID: %s', 'textdomain' ), esc_html( $current_user->ID ) );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0