admin管理员组文章数量:1333201
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 ' '. 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 ' '. 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
1 Answer
Reset to default 0Use 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 ' '. (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');
本文标签: phpDisplay a custom name when the user has no name settle in his account
版权声明:本文标题:php - Display a custom name when the user has no name settle in his account 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742286921a2447094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论