0

I would like to have a new user role (or restrict a role which is already available from Wordpress) which can only manage media (upload, delete pictures etc.) in the wordpress backend - every other possibility in the wordpress backend is not allowed and not visible.

Used user role editor without success

3
  • If I'm reading this right, the only capability your Media Manager role would require is upload_files.
    – Pat J
    Commented Feb 18, 2023 at 16:57
  • 1
    delete_posts also for deleting files! thanks for your input.
    – Lothar
    Commented Feb 19, 2023 at 22:07
  • Yes, I saw that in @Harrison's answer. You learn something new every day.
    – Pat J
    Commented Feb 19, 2023 at 23:00

1 Answer 1

1

The add_role function should give you what you're looking for. Here's an example of using it to add a new role that allows the user to upload and delete media:

function wpse413985_add_role(){

/* Check if the role already exists, since we 
don't need to add it again every time the site loads. */
  if ( !get_role( 'wpse413985_media_manager' ) ){
    
     add_role( 
      'wpse413985_media_manager', 
      __( 'Media Manager', 'wpse413985-textdomain' ),
      array( 
        'upload_files' => true,
        'delete_posts' => true
      ));

  }
}

add_action( 'init', "wpse413985_add_role" );

A user with the 'media manager' role in this example will see a link to the media library but none of the other admin screens. The delete posts capability is necessary to delete pictures because media files count as 'posts' in wordpress. The user won't be able to delete regular posts because they can't visit that part of the admin.

This function would go in a theme's functions.php. However, you could also put it in a plugin. In that case, instead of hooking to the init action, you could use register_activation_hook. That would eliminate the need for the conditional.

1
  • Thanks Harrison, didnt know that delete posts was necessary too!
    – Lothar
    Commented Feb 19, 2023 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.