Display first name of logged in user?

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.

Topic username shortcode functions Wordpress

Category Web


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'); 

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 ) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.