You absolutely did not understand how the code from my previous answer works, yeah? The PHP ternary operator $result = $a ? $b : $c
works the same way as the following construction:
if ( $a ) {
$result = $b;
} else {
$result = $c;
}
So the whole idea was to check if the get_user_meta(get_current_user_id(), 'last_name', true)
function returns an empty value. When you change it to
($username = get_user_meta(get_current_user_id(), 'last_name', true) . '</a>') ? ...
you break the whole logic, because that
get_user_meta(get_current_user_id(), 'last_name', true) . '</a>'
string would never have an empty value (that should be quite obvious, even if the get_user_meta()
function returns an empty value, the whole string would be equal to '</a>'
). Use this statement:
return ' <a href="' . admin_url('profile.php') . '">' . ( ( $username = get_user_meta( get_current_user_id(), 'last_name', true ) ) ? $username : ">Noe Name [edit profile]" ) . '</a>';
Update
If you want to work with several user meta fields, I recommend to use get_userdata()
function:
function colaborador_nome($atts)
{
if (is_user_logged_in() && !is_feed()) {
$userdata = get_userdata( get_current_user_id() );
$username = trim( implode( ' ', array( $userdata->first_name, $userdata->last_name ) ) );
return ' <a href="' . admin_url('profile.php') . '">' . ( $username ? $username : ">Noe Name [edit profile]" ) . '</a>';
}
}
I still don’t understand why the shorthand form of ternary operator doesn’t work for you, using it would help to simplify the return
statement to
return ' <a href="' . admin_url('profile.php') . '">' . ( $username ?: ">Noe Name [edit profile]" ) . '</a>';