3

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;
}
2
  • I don’t understand your question. Could you add a screenshot and mark the element you want to remove?
    – fuxia
    Mar 31, 2011 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. Mar 31, 2011 at 22:38

1 Answer 1

5

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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