Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

On editing the attachments of a post whith at least one attachment previously uploaded, how do I remove the From computer tab and redirect to the Gallery tab?

The "Add an Image" tabs

This is my current code:

add_filter('media_upload_tabs','remove_medialibrary_tabs', 99);
function remove_medialibrary_tabs($tabs) {
    if ($post_id = (isset($_REQUEST['post_id']) ? $_REQUEST['post_id'] : false)) {
        if (count(get_posts("post_type=attachment&post_parent={$post_id}"))>0) {
            // MY QUESTION
        }
    }

    unset($tabs['type_url']);
    unset($tabs['library']);

    return $tabs;
}
share|improve this question
    
I don’t understand your question. Could you add a screenshot and mark the element you want to remove? –  toscho Mar 31 '11 at 8:13
    
When you edit some post, theres an icon to attach images or media to a post. On that modal window, I need to redirect the user to the Gallery tab (not the From computer tab) if the post has at least one attachmet previously uploaded. –  José Pablo Orozco Marín Mar 31 '11 at 22:38
add comment

1 Answer

up vote 5 down vote accepted

To remove the From Computer tab header, you unset the type key from that array. However, this will (confusingly) not remove the tab content, and because this is the default tab it will show it even if the tab header for it is gone.

To change the default tab you must hook into the media_upload_default_tab filter. This gets called in multiple places, I did not research which one is called in which circumstances, so I moved the check for attachments to a separate function and rewrote your code like this:

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;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.