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.

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 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.

Auteur
Publications
#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: 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'); 
 }
 }
 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 🙂