Skip to content

Instantly share code, notes, and snippets.

@mbijon
Last active July 10, 2023 23:30
Show Gist options
  • Save mbijon/7311081 to your computer and use it in GitHub Desktop.
Save mbijon/7311081 to your computer and use it in GitHub Desktop.
Remove 'Add Media' button from above WP editor, per post-type
function check_post_type_and_remove_media_buttons() {
global $current_screen;
// use 'post', 'page' or 'custom-post-type-name'
if( 'post' == $current_screen->post_type ) add_action( 'media_buttons_context' , create_function('', 'return;') );
}
add_action('admin_head','check_post_type_and_remove_media_buttons');
@dbshoupe
Copy link

dbshoupe commented Apr 5, 2016

This did not work for me in WordPress 4.4.2.24. Had to change it to below:

function check_post_type_and_remove_media_buttons() {
global $current_screen;
// use 'post', 'page' or 'custom-post-type-name'
if( 'services' == $current_screen->post_type ) remove_action('media_buttons', 'media_buttons');
}
add_action('admin_head','check_post_type_and_remove_media_buttons');

@mrdulal
Copy link

mrdulal commented Nov 28, 2016

@dbshoupe. Code work for me. Thank you.

@nfsarmento
Copy link

Thanks @dbshoupe thats works for me.

@deadhead1971
Copy link

Thanks @dbshoupe this worked for me.
How would it need to be modified to check for more than one custom post type please?
thanks

@gchtr
Copy link

gchtr commented Aug 27, 2018

I remove the media buttons by setting the media_buttons setting to false in the wp_editor_settings filter:

/**
 * Removes media buttons from post types.
 */
add_filter( 'wp_editor_settings', function( $settings ) {
    $current_screen = get_current_screen();

    // Post types for which the media buttons should be removed.
    $post_types = array( 'post' );

    // Bail out if media buttons should not be removed for the current post type.
    if ( ! $current_screen || ! in_array( $current_screen->post_type, $post_types, true ) ) {
        return $settings;
    }

    $settings['media_buttons'] = false;

    return $settings;
} );

@deadhead1971 You could add your own post types to the $post_types array if you want to have a check for more than one post type.

@andersonnarciso
Copy link

@gchtr thanks!

@jorellana818
Copy link

I remove the media buttons by setting the media_buttons setting to false in the wp_editor_settings filter:

/**
 * Removes media buttons from post types.
 */
add_filter( 'wp_editor_settings', function( $settings ) {
    $current_screen = get_current_screen();

    // Post types for which the media buttons should be removed.
    $post_types = array( 'post' );

    // Bail out if media buttons should not be removed for the current post type.
    if ( ! $current_screen || ! in_array( $current_screen->post_type, $post_types, true ) ) {
        return $settings;
    }

    $settings['media_buttons'] = false;

    return $settings;
} );

@deadhead1971 You could add your own post types to the $post_types array if you want to have a check for more than one post type.

This worked for me. Thank you!

@trublud
Copy link

trublud commented Jul 10, 2023

Here is another way
$settings = array('textarea_name'=>'Overview[Notes]') ;
$settings['media_buttons'] = false;
wp_editor( htmlspecialchars_decode($text), 'mettaabox_ID2', $settings );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment