php - Shortcode to embed Edit Account form not working

I'm creating a short code that shows both first and last name of the user if he's logged in, or, if he's

I'm creating a short code that shows both first and last name of the user if he's logged in, or, if he's logged in, but hasn't configured any name, it will return a random name with a message telling him to edit his profile. I'm succeeding into getting the names, but not into showing the text that should appear in case the user has no name. The code:

// Show name if logged in - PT e ENG
function colaborador_nome($atts)
{
    if (is_user_logged_in() && !is_feed()) {
          return '&nbsp;' .    $username = '<a href="' . ( admin_url('profile.php') .'">' . get_user_meta(get_current_user_id(), 'first_name' , true) . '&nbsp;' .
        (($username = get_user_meta(get_current_user_id(), 'last_name', true)  . '</a>')  ? $username : '&nbsp;<a href="' . admin_url('profile.php') . '">Noe Name [edit profile]</a>"'));   
    }
}
add_shortcode('colaborador_nome', 'colaborador_nome');

The part that's not working:

 ? $username : '&nbsp;<a href="' . admin_url('profile.php') . '">Noe Name [edit profile]</a>"'

I'm creating a short code that shows both first and last name of the user if he's logged in, or, if he's logged in, but hasn't configured any name, it will return a random name with a message telling him to edit his profile. I'm succeeding into getting the names, but not into showing the text that should appear in case the user has no name. The code:

// Show name if logged in - PT e ENG
function colaborador_nome($atts)
{
    if (is_user_logged_in() && !is_feed()) {
          return '&nbsp;' .    $username = '<a href="' . ( admin_url('profile.php') .'">' . get_user_meta(get_current_user_id(), 'first_name' , true) . '&nbsp;' .
        (($username = get_user_meta(get_current_user_id(), 'last_name', true)  . '</a>')  ? $username : '&nbsp;<a href="' . admin_url('profile.php') . '">Noe Name [edit profile]</a>"'));   
    }
}
add_shortcode('colaborador_nome', 'colaborador_nome');

The part that's not working:

 ? $username : '&nbsp;<a href="' . admin_url('profile.php') . '">Noe Name [edit profile]</a>"'
Share Improve this question asked Jul 9, 2020 at 3:31 023023023023 1351 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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 '&nbsp;<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 '&nbsp;<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 '&nbsp;<a href="' . admin_url('profile.php') . '">' . ( $username ?: ">Noe Name [edit profile]" ) . '</a>';

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

相关推荐

  • php - Shortcode to embed Edit Account form not working

    I'm creating a short code that shows both first and last name of the user if he's logged in, or, if he's

    21小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信