wordpress snippets last updated   : January 29, 2015
we now have 646 snippets

Remove “From computer” tab for posts with existing attachments

wordpress snippet

Adding this snippet to the functions.php of your wordpress theme will remove the “From computer:” tab from posts within existing attachments.

( Example screenshot )
snippet :  PHPcopy
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;
}
source →

Did you like this post? If so please share it with your friends. Thanks!

  • http://web-devise.com/ ADv

    Does this snippet work on WP 3.8?

  • http://web-devise.com/ ADv

    Does this snippet work on WP 3.8?