Want to remove the Add Media button from the WordPress post editor screen, but only for specific custom post types?

Sometimes it’s not enough to give instructions and make training materials for client sites. Sometimes we need to go further by physically restricting the options available on screenm by hiding certain buttons!

Here’s the scenario:

I had built a members-only section for a site, in which there were several groups that people could be a member of. These secret pages, inaccessible to the public, also had a custom way of presenting file downloads, with encrypted URL’s to make sure that they could not be accessed without being logged in, and that people could not share download links with people who were not already group members.

The system worked perfectly, and the site editors were given some training materials on how to add their file attachments to these secret pages, because it was slightly different from creating a regular WordPress post.

At first, it went very well but after a few months people started to add files using the ordinary Add Media link from the post editor. This was probably just out of habit – I’m sure it would be a really simple mistake to make.

But this was an issue, as we couldn’t have that content being at risk of being accessed by people who weren’t members of the groups.

So we did a little clean up, deleting links and replacing them with the custom method to retain file privacy.

But then I wondered, what’s to stop people making the same mistake again in future?

Wouldn’t it be better to try to make it that for those particular custom groups, it would not actually be possible to add files to posts via the Add Media button?

Fortunately with a little bit of code, we can remove the Add Media buttons from the dashboard, just on the specific custom post pages that we choose.

Simply add the following code to your theme’s functions.php file


function check_post_type_and_remove_media_buttons() {
global $current_screen;
// Replace following array items with your own custom post types
$post_types = array('cpt-1-slug','cpt-2-slug','cpt-3-slug');
if (in_array($current_screen->post_type,$post_types)) {
remove_action('media_buttons', 'media_buttons');
}
}
add_action('admin_head','check_post_type_and_remove_media_buttons');

Once you’ve added the code and amended the slugs to reflect your own custom post types, the media buttons will have gone!

>