Help With Code – in_array – Check for two or more items

Luís provided me with some code that works great. I want to change it so it looks for two or more user roles. I need some confirmation that I’ve made the changes correctly. I’ll paste the original code, followed by the modified code:

add_action('admin_head', 'hidepopup');
function hidepopup(){
$user = wp_get_current_user();
if ( in_array( 'contributor', (array) $user->roles ) ) {
echo '<style>
.menu-icon-inc_popup{
display: none;
}
</style>';
}
}

Modified

add_action('admin_head', 'hidepopup');
function hidepopup(){
$user = wp_get_current_user();
if ( in_array( 'contributor' || 'contributor-modified', (array) $user->roles ) ) {
echo '<style>
.menu-icon-inc_popup{
display: none;
}
</style>';
}
}

Does line 4 look correct (i.e. || ‘contributor-modified’:wink:?

Thanks for your help

Chris

  • Nithin Ramdas
    • Support Wizard

    Hi Chris,

    Hope you are doing good today. :slight_smile:

    It seems like you want to check for more than one user roles, so you can try the following code:

    add_action('admin_head', 'hidepopup');
    function hidepopup(){
    $user = wp_get_current_user();

    $roles_to_check = array( 'contributor', 'contributor-modified' );
    $result = !empty( array_intersect( $roles_to_check, (array) $user->roles) );

    if ( $result ) {
    echo '<style>
    .menu-icon-inc_popup{
    display: none;
    }
    </style>';
    }
    }

    The above code will look for user roles contributor, contributor-modified, and subscriber. You can add new roles in that array, and it should work fine.

    Please let us know if you still need any further assistance. Have a nice day. :slight_smile:

    Kind Regards,

    Nithin