Question

I'm in WordPress. I need to compare two arrays, then unset any matches from one of them. The trouble is, one of the arrays is gettings its data from get_users, so I think I might have to convert it to strings using foreach, so I can tell it to give the user_login for the users in the array. Unless I'm wrong about that, I think what I need to be able to do is take the array, do a foreach statement so I can tell it to grab the user_logins, then convert them all back to an array. Here's all I have so far (and I'm not sure about whether I'm doing the if statement correctly in there (whether "null" is the right qualifier):

$adminnames = get_users('role=administrator');
$result = array_intersect($adminnames, $username);
if($result !== null){unset($username[$result]);}

$username is one of the attributes in the shortcode.

Also, forgive my fuzziness, if there's only one person in "username" does that mean it's not an array? 'Cause if so that might mess this up.

-- UPDATE --

If the only way to get the user_login of all administrators is to do a foreach then echo it, this might not even be possible.

Was it helpful?

Solution

I found a solution that works just great. I already do a preg_split on the $username attribute, so after I run that, this is what I've done to unset administrator usernames from the $username attribute:

$users = preg_split("/[\s,]+/",$username);
wp_get_current_user();
if(current_user_can(administrator)){
        $nohidename = array_search($current_user->user_login,$users);
        if($nohidename !== FALSE){unset($users[$nohidename]);
        }
}

So it does it just based on whether the current user is an administrator. If not, it leaves it as is. Works great.

EDIT - An even simpler way to do it, without the array_search:

if(current_user_can(administrator)){
        if($username){
            unset($username);
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top