OunknownO
OunknownO

Reputation: 1206

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" }

Upvotes: 0

Views: 52

Answers (3)

lipika chakraborty
lipika chakraborty

Reputation: 199

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.

Upvotes: 0

Starfish
Starfish

Reputation: 3574

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] == '')

Upvotes: 0

nospor
nospor

Reputation: 4220

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 );

Upvotes: 2

Related Questions