• Resolved svaika

    (@svaika)


    Hi,
    This question was asked 3 years ago at this topic: Press here

    So I just paste the same question in quotes. I was trying to user get_userdata(); instead of get_avatar( $id ); but it didn’t work. Any ideas how to solve that issue?

    “I’m trying to create a simple shortcode that will show a user’s display name based on the user ID you insert (or default to the currently logged in user if no ID is provided). Below is a similar shortcode I’ve created to do this for a user’s avatar:

    <?php
    
    function avatar_shortcode( $atts ) {
    
    global $current_user;
    $user_id = ( is_user_logged_in() ? $current_user->ID : NULL );
    
    extract( shortcode_atts( array(
    'id' => $user_id,
    ), $atts ) );
    
    return get_avatar( $id );
    }
    
    add_shortcode('avatar', 'avatar_shortcode');
    
    ?>

    [avatar] displays the currently logged in user’s avatar and [avatar id=”1″] display’s the avatar for the user with an ID of 1 and so on. This is exactly what I am looking for to show a user’s display name. Something where [display-name] will show the currently logged in user’s display name and [display-name id=”1″] will show the display name for the user with an ID of 1 and so on.”

    Thanks everyone in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter svaika

    (@svaika)

    Any ideas how to solve it?

    Thread Starter svaika

    (@svaika)

    Here is a code:

    function name_shortcode( $atts ) {
    
    global $current_user;
    $user_id = ( is_user_logged_in() ? $current_user->ID : NULL );
    
    extract( shortcode_atts( array(
    'id' => $user_id,
    ), $atts ) );
    
    return get_userdata($id)->user_login;
    }
    
    add_shortcode('name', 'name_shortcode');

    May someone will be useful. To get user name use shortcode [name id=”here_insert_user_id”]

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Shortcode to show a user's display name’ is closed to new replies.