Allow contributors to upload images but not delete them

Rate this post

I enabled image uploads for contributors on my WordPress site via the following code:

if ( current_user_can('contributor') && !current_user_can('upload_files') )
    add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
    $contributor = get_role('contributor');

It works great, but I also need to disallow them to delete the images they uploaded. Is there an easy way to do it? I know there’s User Role Editor plugin, but I don’t want to install it just for this.



You can use delete_attachment hook or pre_delete_attachment filter for that:

add_action('delete_attachment', 'wp66410923_prevent_delete_attachment', 11, 1);
function wp66410923_prevent_delete_attachment($postID)
  if (current_user_can('contributor')) {
    echo __('You can not delete attachments.');

add_filter( 'pre_delete_attachment', 'wp66410923_filter_attachment_deletion', 10, 3 );
function wp66410923_filter_attachment_deletion( $delete, $post, $force_delete ){
  if (current_user_can('contributor')) {
    return false;
  return true;

Leave a Reply

Your email address will not be published.

Back to top button