php - Display a custom name when the user has no name settle in his account

I'm creating a code that shows the First Name for logged in users, using a shortcode, but I noticed that if the use

I'm creating a code that shows the First Name for logged in users, using a shortcode, but I noticed that if the user has no name settle, it will display a blank space. How can I set a custom name to show up instead of the blank space? Something like this "Hi >No Name<, please settle your name in your account."

Display first name:

// show first name if logged in
function colaborador_nome($atts) { if (is_user_logged_in() && !is_feed()) { return '&nbsp;'. get_user_meta( get_current_user_id(), 'first_name', true ); }
 }
add_shortcode('colaborador_nome', 'colaborador_nome'); 

I'm creating a code that shows the First Name for logged in users, using a shortcode, but I noticed that if the user has no name settle, it will display a blank space. How can I set a custom name to show up instead of the blank space? Something like this "Hi >No Name<, please settle your name in your account."

Display first name:

// show first name if logged in
function colaborador_nome($atts) { if (is_user_logged_in() && !is_feed()) { return '&nbsp;'. get_user_meta( get_current_user_id(), 'first_name', true ); }
 }
add_shortcode('colaborador_nome', 'colaborador_nome'); 
Share Improve this question edited Jul 7, 2020 at 3:37 023023 asked Jul 7, 2020 at 2:24 023023023023 1351 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use PHP ternary comparsion operator to check if get_user_meta() function returns blank or non-blank result:

function colaborador_nome($atts) {
    if (is_user_logged_in() && !is_feed()) {
        return '&nbsp;'. (get_user_meta( get_current_user_id(), 'first_name', true ) ?: "Hi >No Name<, please settle your name in your account.");
    }
}
add_shortcode('colaborador_nome', 'colaborador_nome'); 

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

相关推荐

  • php - Display a custom name when the user has no name settle in his account

    I'm creating a code that shows the First Name for logged in users, using a shortcode, but I noticed that if the use

    20小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信