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');
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 ' '. (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');
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0