Skip to content

Instantly share code, notes, and snippets.

@nydame
Last active May 20, 2018 16:25
Show Gist options
  • Save nydame/7c4901d00944cb411251e0df1d2ed3a7 to your computer and use it in GitHub Desktop.
Save nydame/7c4901d00944cb411251e0df1d2ed3a7 to your computer and use it in GitHub Desktop.
Control who can delete Media Library files on a WordPress site on the basis of a capability
</php
// In this example, only logged-in users with the 'activate_plugins' capability will be able to delete files shown in the Media Library.
// (By default, only administrators have this capability.)
// This code should be put in functions.php or in a plugin.
add_action('delete_attachment', array($this, 'restrict_media_deletion_permissions'), 1);
public function restrict_media_deletion_permissions($post_id) {
if (! current_user_can('activate_plugins')) {
?>
<script type="text/javascript">alert("You do not have permission to delete media files.");</script>
<?php
die();
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment