Skip Navigation

[Résolu] How to hide Add Media button for specific custom post types

Ce fil est résolu. Voici une description du problème et la solution proposée.

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

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:

Relevant Documentation:

This support ticket is created Il y a 4 années et 5 mois. 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.

Marqué : 

This topic contains 2 réponses, has 2 voix.

Last updated by Alan Il y a 4 années et 5 mois.

Assigned support staff: Minesh.



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

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




Languages: Anglais (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'); 

More info:
=> hidden link


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