简体   繁体   中英

if statement condition doesn't work when comparing the value from array

I have a problem, this is my code and the goal of it is that after login it redirects to one or another page.

function my_login_redirect(){
    $current_user = wp_get_current_user();
    $user1 = $current_user->ID;
    $meta = get_user_meta($user1, 'kundenumber');

    if ($meta == '' ) {
        // return the url that the login should redirect to
        $var = 'http://www.example1.com';
    } else {
        $var = 'http://www.example2.com';
    }

    return $var;
}

It always returns the first link (I have two users these are they var dumps for 'kundenumber' field)

 array(1) { [0]=> string( 0 ) "" }
 array(1) { [0]=> string( 6 ) "123456" }

https://codex.wordpress.org/Function_Reference/get_user_meta

So you need to set third parameter as true to get value not array :

$meta = get_user_meta($user1, 'kundenumber', true );

get_user_meta() returns an array. You're comparing the whole array to an empty string. You should compare the string in the array to an empty string like if ($meta[0] == '')

This means $meta is empty. There are two possibility for empty $meta - 1)Use of wrong key. 2)$user_id is not set or is set to an incorrect value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM