functions - 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 hav

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.

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.

Share Improve this question edited Jul 6, 2020 at 22:32 023023 asked Jul 6, 2020 at 16:24 023023023023 1351 silver badge7 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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'); 

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742287373a4415563.html

相关推荐

  • functions - 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 hav

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信