Skip Navigation

[Resolved] How to hide Add Media button for specific custom post types

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to check multiple post types with if condition and hide media button conditionally

Solution:
You can use PHP in_array() function to check multiple post types using if condition.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/how-to-hide-add-media-button-for-specific-custom-post-types/#post-733184

Relevant Documentation:
https://www.geeksforgeeks.org/php-in_array-function/

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Alan 5 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#732842

Hi,

I need to hide the Add Media button for specific custom post types.

I found this code on github, which works for a single custom post type:

function check_post_type_and_remove_media_buttons() {
 global $current_screen;
 if ( 'CPT slug goes here' == $current_screen->post_type ) remove_action('media_buttons', 'media_buttons'); 
 }
 add_action('admin_head','check_post_type_and_remove_media_buttons');

Simple question (I hope) - how to modify this code to check against multiple custom post types (by their specific slug names)?

thanks

#733184

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - you can use in_array function to check against multiple post types.

For example:

function check_post_type_and_remove_media_buttons() {
 global $current_screen;

// Replace following array items with your post type slug
 $post_types = array('post-type-slug-1','post-type-slug-2','post-type-slug-3');

 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');

More info:
=> hidden link

#734012

I knew that an array would be the answer, but just wasn't sure how to make it happen.
Fantastic - thank you 🙂

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.