Beata
Beata

Reputation: 97

remove admin menu if current user has BOTH 'editor' and 'contributor' roles

Due to specifics of my WordPress, I need to remove the 'Posts' admin menu item for users who have both Editor and Contributor roles.

I have this code but now it removes this menu item if user has editor or contributor as one of their roles. And I need this behavior only if both these roles are assigned to current user.

How can I change my code to make it work like this?

function hide_posts_in_admin_menu() : void {

    if ( ! function_exists( 'current_user_has_role' ) ) {

        function current_user_has_role( $role ) {

            $user = get_userdata( get_current_user_id() );
            if ( ! $user || ! $user->roles ) {
                return false;
            }

            if ( is_array( $role ) ) {
                return array_intersect( $role, (array) $user->roles ) ? true : false;
            }

            return in_array( $role, (array) $user->roles, true );
        }
    }

    if ( ! current_user_has_role( array( 'editor', 'contributor' ) ) ) {
        return;
    }

    remove_menu_page( 'edit.php' );
}

Thank you!

Upvotes: 0

Views: 654

Answers (1)

Ashok kumawat
Ashok kumawat

Reputation: 551

you can use this code for it


function hide_menu() {

$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) || in_array( 'contributor', (array) $user->roles ) ) {
    
      remove_menu_page( 'edit.php' ); //Posts
    }

}
add_action('admin_head', 'hide_menu');

Upvotes: 1

Related Questions