Remove upload_files capability from a role but allow role to manage an avatar image

Different hints to your idea to create a solution for your topic.

  1. Since longer time, WP use always role names, no level.
    Use always the role name ot the capability for a check in the access topic. The function current_user_can() should helps you.

  2. To remove a menu item, use the wp core function remove_menu_page( $menu_slug ); and not the unset. Is more stable and supported.

  3. The hook admin_head is not the best hook to init changes on menu or remove cap. To remove cap use the init hook or similar, early on the load stack. For the menu item use admin_menu.

To your question

Is it possible to add_cap() just for the one module?

No, the capability is globally. But you can check the site, the screen in the back end and remove it only on this screen, if this is helpful and possible. If you you will check the page of the packend, use the function https://codex.wordpress.org/Function_Reference/get_current_screen. You get an array with a lot of information about the page and use this for a statement to set the capability.

In your topic, I think it, that you should remove the capability upload_files with your idea – $wp_roles->remove_cap( 'author', 'upload_files' );. Check the avatar or profile page and add this capability for your users.

Leave a Comment