Disable “Add Media” Button for Everyone but Admin

Recently I was working on a project where I was suppose to disable the “Add Media” button for authors from the post editing window, so that they can’t upload the image on the posts. As you all know by now, by default in WordPress user role has the capability of adding images. However, there are ways to disable such feature.

One of the easiest solution is to remove the capability of uploading files from the system (WordPress) itself. You can easily do this by adding the following line on your theme’s functions.php file and simply refreshing any page from front-end.

<?php
$role = get_role('author');
$role->remove_cap('upload_files');
?>

What is happening here is, we are asking WordPress to find a role (“author” in this case) and then remove the capability (upload_files). However, we always need to remember that these setting gets saved into the database and doesn’t need to perform such function over and over again. I briefly discussed about this issue in one of my post. Just in case if you remove any capability mistakenly, don’t worry, it can be added later on using add_cap function.

If you need to disable the “Add Media” button from other users but the Administrator of your site. Simply copy and paste the following snippet.

<?php
function disable_media_button() {
   global $current_user;
   get_currentuserinfo();
   if($current_user->user_level = 2) {
      remove_action('media_buttons','media_buttons');} }
add_action('admin_head','disable_media_button');

In this case, we are checking if current user belongs to user level “2” or not. Since the “Author” has the user level of “2”, it would remove the capability.

Now, lets take a look at another version of the same function.

function disable_media_button() {
   if(!current_user_can('manage_options')) {
      remove_action('media_buttons','media_buttons');}}
add_action('admin_head','disable_media_button');

In this case, we are checking if the current logged in user has the administrative capability (manage_options) or not, if the user doesn’t have the capability this function should disable the “Add Media” button from post editing window.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.