admin管理员组

文章数量:1122846

I have this code, which works perfectly:

    if (in_array('administrator', $user->roles)) {

I need it to also check for another role, so that it would be an, 'if this or that' scenario, such as, if administrator or editor

I've tried:

if (in_array('administrator', 'editor' $user->roles)) {

But that didn't work. Any help is appreciated.

I have this code, which works perfectly:

    if (in_array('administrator', $user->roles)) {

I need it to also check for another role, so that it would be an, 'if this or that' scenario, such as, if administrator or editor

I've tried:

if (in_array('administrator', 'editor' $user->roles)) {

But that didn't work. Any help is appreciated.

Share Improve this question asked May 27, 2024 at 21:23 BarkingBirdBarkingBird 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If I understand you correctly, you want to determine if the user has any role that is in some pre-defined list like this:

$accepted_roles = array(
    'administrator',
    'editor',
);

if ( ! empty( array_intersect( $accepted_roles, $user->roles ) ) ) {
   ...

本文标签: phpIf user is Admin or another role