• Using the old Kubrick-based default theme, v1.6.
    We have a bunch of Authors posting URLs to their work, but some keep posting via From Computer tab.

    I’d like to turn off From Computer and From Media Library tabs and set From URL to the default (only) tab.

    Superann had this on her site: add_filter('media_upload_tabs','remove_medialibrary_tab');

    Hax had this:
    add_filter( 'media_upload_tabs', 'media_upload_tabs'); //hide media tabs

    Also: I can’t find the tabs in this old theme. They don’t seem to be on functions.php and I’m at a bit of a loss. Anyone able to take this home for me?

    Help would be much appreciated.
    Thanks,
    KIB

Viewing 15 replies - 1 through 15 (of 16 total)
  • the media_upload_tabs filter passes a single argument containing an array of the tabs, you can unset() them to remove them.

    The array looks something like this:

    Array
    (
        [type] => From Computer
        [type_url] => From URL
        [gallery] => Gallery
        [library] => Media Library
    )

    to remove the media library tab:

    function remove_media_library_tab($tabs) {
        unset($tabs['library']);
        return $tabs;
    }
    add_filter('media_upload_tabs', 'remove_media_library_tab');

    If you only want to hide it on certain posts or post_types you can check the $_REQUEST or $_GET args. When using the ‘Upload/Insert’ links several points of data are passed along: /wp-admin/media-upload.php?post_id=28506&type=image&TB_iframe=1&width=640&height=581

    function remove_media_library_tab($tabs) {
        if (isset($_REQUEST['post_id'])) {
            $post_type = get_post_type($_REQUEST['post_id]);
            if ('post' == $post_type)
                unset($tabs['library']);
        }
        return $tabs;
    }
    add_filter('media_upload_tabs', 'remove_media_library_tab');

    Hope that helps.

    Thread Starter ikedabarry

    (@ikedabarry)

    Very helpful, totels. Thank you!

    It worked for me to remove media gallery,
    But it’s still showing gallery tab.
    Is there some way to remove it ?

    For all those who cannot remove gallery tab using media_upload_tabs filter, mentioned above by totels:

    If you look into media.php file you will see that WordPress uses this filter after it is declared to add gallery tab to posts with attachments. So if you simply unset gallery tab it will be added anyway. To avoid this you should use $priority parameter when triggering the filter. The code should look like that:

    function remove_media_tab($tabs) {
    unset($tabs['gallery']);
    return $tabs;
    }
    add_filter('media_upload_tabs','remove_media_tab', 99);

    This code hides all posts and media that do not belong to the currently logged in author (you can change it to apply to other user roles). It also fixes the post and media count on the filter bars (e.g. All | Images | Videos | Unattached).

    //
       // Mostrar sólo las Publicaciones, Comentarios y Multimedia que sean  Propios
       // Más info en > PC-SERVEIS, Dise?o Web - https://www.pc-serveis.es
       //
    
       // Show only posts and media related to logged in author
       add_action('pre_get_posts', 'query_set_only_author' );
       function query_set_only_author( $wp_query ) {
         global $current_user;
         if( is_admin() && !current_user_can('edit_others_posts') ) {
            $wp_query->set( 'author', $current_user->ID );
            add_filter('views_edit-post', 'fix_post_counts');
            add_filter('views_upload', 'fix_media_counts');
         }
       }
    
       // Fix post counts
       function fix_post_counts($views) {
        global $current_user, $wp_query;
        unset($views['mine']);
        $types = array(
            array( 'status' =>  NULL ),
            array( 'status' => 'publish' ),
            array( 'status' => 'draft' ),
            array( 'status' => 'pending' ),
            array( 'status' => 'trash' )
        );
        foreach( $types as $type ) {
            $query = array(
                'author'      => $current_user->ID,
                'post_type'   => 'post',
                'post_status' => $type['status']
            );
            $result = new WP_Query($query);
            if( $type['status'] == NULL ):
                $class = ($wp_query->query_vars['post_status'] == NULL)  ? ' class="current"' : '';
                $views['all'] = sprintf(__('<a href="%s"'. $class  .'>Todas <span class="count">(%d)</span></a>', 'all'),
                    admin_url('edit.php?post_type=post'),
                    $result->found_posts);
            elseif( $type['status'] == 'publish' ):
                $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
                $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Publicadas <span class="count">(%d)</span></a>', 'publish'),
                    admin_url('edit.php? post_status=publish&post_type=post'),
                    $result->found_posts);
            elseif( $type['status'] == 'draft' ):
                $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
                $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Borrador'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),
                    admin_url('edit.php?post_status=draft&post_type=post'),
                    $result->found_posts);
            elseif( $type['status'] == 'pending' ):
                $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
                $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pendiente <span class="count">(%d)</span></a>', 'pending'),
                    admin_url('edit.php?post_status=pending&post_type=post'),
                    $result->found_posts);
            elseif( $type['status'] == 'trash' ):
                $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
                $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Papelera <span class="count">(%d)</span></a>', 'trash'),
                    admin_url('edit.php?post_status=trash&post_type=post'),
                    $result->found_posts);
            endif;
          }
          return $views;
        }
    
       // Fix media counts
       function fix_media_counts($views) {
          global $wpdb, $current_user, $post_mime_types,  $avail_post_mime_types;
          $views = array();
          $count = $wpdb->get_results( "
             SELECT post_mime_type, COUNT( * ) AS num_posts
             FROM $wpdb->posts
             WHERE post_type = 'attachment'
             AND post_author = $current_user->ID
             AND post_status != 'trash'
             GROUP BY post_mime_type
          ", ARRAY_A );
          foreach( $count as $row )
    				if ($count && $row != 0) {
            $_num_posts[$row['post_mime_type']] = $row['num_posts'];
        		$_total_posts = array_sum($_num_posts);
        		$detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
    				};
          if ( !isset( $total_orphans ) )
             $total_orphans = $wpdb->get_var("
                SELECT COUNT( * )
                FROM $wpdb->posts
                WHERE post_type = 'attachment'
                AND post_author = $current_user->ID
                AND post_status != 'trash'
                AND post_parent < 1
             ");
    		if ($matches != 0) {
    		$matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
        foreach ( $matches as $type => $reals )
            foreach ( $reals as $real )
                $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
        $class = ( empty($_GET['post_mime_type']) && !$detached && !isset($_GET['status']) ) ? ' class="current"' : '';
        $views['all'] = "<a href='upload.php'$class>" . sprintf( __('Todas <span class="count">(%s)</span>', 'uploaded files' ), number_format_i18n( $_total_posts )) . '</a>';
        foreach ( $post_mime_types as $mime_type => $label ) {
            $class = '';
            if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
                continue;
            if ( !empty($_GET['post_mime_type']) && wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
                $class = ' class="current"';
            if ( !empty( $num_posts[$mime_type] ) )
                $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . '</a>';
        }
          $views['detached'] = '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) . '>' . sprintf( __( 'Sin Adjuntar <span class="count">(%s)</span>', 'detached files' ), $total_orphans ) . '</a>';
        return $views;
        }
       };

    Greetings from Barcelona ! !

    PC-SERVEIS, Dise?o Web
    https://www.pc-serveis.es

    Thread Starter ikedabarry

    (@ikedabarry)

    Muchas gracias, PCServeis! Voy a mirar en este código.

    hi pcserveis,

    your codes work for the hiding part, but seem the count fix is not working.
    I don’t know if i am the only one who got this error?

    ok..figure it out…turn out i forget to change the post-type…great code..

    Hi, pcserveis,

    Is it possible to apply your code to custom post types?

    I have a several custom posts types

    example (vendors) post_type=vendors

    I tried simply replacing the post_type=post with post_type=vendors but site goes blank on refresh.

    Any help is appreciated.

    nbiz,
    I think u should change the admin_url and as well…
    From
    admin_url('edit.php? post_status=publish&post_type=post'),
    to
    admin_url('edit.php? post_status=publish&post_type=vendors'),

    and don’t forget the post-type in the array

    'post_type' => 'your post type',

    Hope this help

    Hi WP_Dummy, thanks for the input. I have made the changes, but still the changes appear on the ‘posts’ admin page but not the ‘vendors’ admin page. Here is the code:

    // Show only posts and media related to logged in author
       add_action('pre_get_posts', 'query_set_only_author' );
       function query_set_only_author( $wp_query ) {
         global $current_user;
         if( is_admin() && !current_user_can('edit_others_posts') ) {
            $wp_query->set( 'author', $current_user->ID );
            add_filter('views_edit-post', 'fix_post_counts');
            add_filter('views_upload', 'fix_media_counts');
         }
       }
    
       // Fix post counts
       function fix_post_counts($views) {
        global $current_user, $wp_query;
        unset($views['mine']);
        $types = array(
            array( 'status' =>  NULL ),
            array( 'status' => 'publish' ),
            array( 'status' => 'draft' ),
            array( 'status' => 'pending' ),
            array( 'status' => 'trash' )
        );
        foreach( $types as $type ) {
            $query = array(
                'author'      => $current_user->ID,
                'post_type'   => 'vendors',
                'post_status' => $type['status']
            );
            $result = new WP_Query($query);
            if( $type['status'] == NULL ):
                $class = ($wp_query->query_vars['post_status'] == NULL)  ? ' class="current"' : '';
                $views['all'] = sprintf(__('<a href="%s"'. $class  .'>Todas <span class="count">(%d)</span></a>', 'all'),
                    admin_url('edit.php?post_type=vendors'),
                    $result->found_posts);
            elseif( $type['status'] == 'publish' ):
                $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
                $views['publish'] = sprintf(__('<a href="%s"'. $class .'>Publicadas <span class="count">(%d)</span></a>', 'publish'),
                    admin_url('edit.php? post_status=publish&post_type=vendors'),
                    $result->found_posts);
            elseif( $type['status'] == 'draft' ):
                $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
                $views['draft'] = sprintf(__('<a href="%s"'. $class .'>Borrador'. ((sizeof($result->posts) > 1) ? "s" : "") .' <span class="count">(%d)</span></a>', 'draft'),
                    admin_url('edit.php?post_status=draft&post_type=vendors'),
                    $result->found_posts);
            elseif( $type['status'] == 'pending' ):
                $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
                $views['pending'] = sprintf(__('<a href="%s"'. $class .'>Pendiente <span class="count">(%d)</span></a>', 'pending'),
                    admin_url('edit.php?post_status=pending&post_type=vendors'),
                    $result->found_posts);
            elseif( $type['status'] == 'trash' ):
                $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
                $views['trash'] = sprintf(__('<a href="%s"'. $class .'>Papelera <span class="count">(%d)</span></a>', 'trash'),
                    admin_url('edit.php?post_status=trash&post_type=vendors'),
                    $result->found_posts);
            endif;
          }
          return $views;
        }

    hi nbiz,

    I checked with my codes, found out one difference,
    Try this:
    At your :

    function query_set_only_author( $wp_query ) {
         global $current_user;
         if( is_admin() && !current_user_can('edit_others_posts') ) {
            $wp_query->set( 'author', $current_user->ID );
            add_filter('views_edit-post', 'fix_post_counts');
            add_filter('views_upload', 'fix_media_counts');
         }
       }

    Change to :

    function query_set_only_author( $wp_query ) {
         global $current_user;
         if( is_admin() && !current_user_can('edit_others_posts') ) {
            $wp_query->set( 'author', $current_user->ID );
            $screen = get_current_screen();
            add_filter('views_'.$screen->id, 'fix_post_counts');
         }
       }

    Note that the ‘views_edit-post’ is replace with ‘views_’.$screen->id.

    Hi WP_Dummy,

    Thanks for the follow up. I have modified code and it still did not work initially.

    But, here is what I found:

    1. I gave the test user the permission (edit_others_posts) but it still didn’t work.

    2. I deleted this section and now it does work:

    if( is_admin() && !current_user_can(‘edit_others_posts’) ) ….. but, now it applies to admins also.

    Is there a way to apply this based on user roles (vendor pro, business pro, etc.?) I use s2 member to create user roles.

    Thanks again for your help.

    How about this

    global $pagenow;
    if(!current_user_can('administrator') && current_user_can('edit_posts') && ('edit.php' == $pagenow))

    This is what I use.

    How i can change the order tabs ?

    to get :
    – my new tab
    – From Computer
    – From URL
    – From Gallery
    – From Media Library

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Hide media upload, library tabs, leave URL tab’ is closed to new replies.