Are you looking for a way to remove the “From computer:” tab from posts within existing attachments? While there’s probably a plugin for this, we have created a quick code snippet that you can use to remove “From computer:” tab for posts in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:
add_filter('media_upload_tabs','wpse13567_media_upload_tabs', 99); function wpse13567_media_upload_tabs( $tabs ) { if ( wpse13567_post_has_attachments() ) { unset( $tabs['type'] ); } unset( $tabs['type_url'] ); unset( $tabs['library'] ); return $tabs; } add_filter( 'media_upload_default_tab', 'wpse13567_media_upload_default_tab' ); function wpse13567_media_upload_default_tab( $tab ){ if ( wpse13567_post_has_attachments() ) { return 'gallery'; } return $tab; } function wpse13567_post_has_attachments(){ static $post_has_attachments = null; if ( null === $post_has_attachments && $post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false) ) { $post_has_attachments = count(get_posts("post_type=attachment&post_parent={$post_id}"))>0; } return $post_has_attachments; }
Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.
If you liked this code snippet, please consider checking out our other articles on the site like: 24 best WordPress education themes and how to set up Google Analytics eCommerce tracking for WooCommerce.
Does this snippet work on WP 3.8?
Does this snippet work on WP 3.8?