1

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.

2 Answers 2

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'); 
12
  • I tried it like this: function colaborator_nome($atts) { if (is_user_logged_in() && !is_feed()) { get_user_meta( get_current_user_id(), 'first_name', true ); } } add_shortcode('colaborator_nome', 'colaborator_nome'); It didn't work.
    – 023023
    Commented Jul 6, 2020 at 22:30
  • You need to add echo. Like this: function colaborator_nome($atts) { if (is_user_logged_in() && !is_feed()) { echo get_user_meta( get_current_user_id(), 'first_name', true ); } } add_shortcode('colaborator_nome', 'colaborator_nome');
    – Fredrik
    Commented Jul 6, 2020 at 22:33
  • Not working. =(
    – 023023
    Commented Jul 6, 2020 at 22:39
  • It's returning a blank space.
    – 023023
    Commented Jul 6, 2020 at 22:40
  • Have you set your first name in your profile? The code is tested and it does work.
    – Fredrik
    Commented Jul 6, 2020 at 22:41
0

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 ) );
7
  • What does textdomain stand for?
    – 023023
    Commented Jul 6, 2020 at 16:59
  • it's a parameter on the __() Wordpress translation function .. I quickly pasted the example code but the main part you need is e.g. $current_user->user_firstname
    – mozboz
    Commented Jul 6, 2020 at 17:28
  • Should it be like this? function colaborator_avatar($atts) { if (is_user_logged_in() && !is_feed()) { return $current_user->user_firstname } } add_shortcode('colaborator_nome', 'colaborator_nome');
    – 023023
    Commented Jul 6, 2020 at 17:30
  • did you try it? does it work?
    – mozboz
    Commented Jul 6, 2020 at 18:55
  • Just tried it, site crashed (error).
    – 023023
    Commented Jul 6, 2020 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.