Accueil › Toolset Professional Support › [Résolu] How to hide Add Media button for specific custom post types
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 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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)
Marqué : Types plugin
This topic contains 2 réponses, has 2 voix.
Last updated by Alan Il y a 5 années et 11 mois.
Assisted by: Minesh.
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
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
I knew that an array would be the answer, but just wasn't sure how to make it happen.
Fantastic - thank you 🙂