6

I'd like to remove list mode from media library and let users see grid-mode only. I've seen it's a matter of

$modes = array( 'grid', 'list' );

in upload.php

but I don't want to mod the core.

1 Answer 1

11

This is a little hacky, but doesn't require you to edit core files, which you should never do, as you are aware.

add_action('admin_init', function() {
    $_GET['mode'] = 'grid';
}, 100);

This will always force the mode into grid view.

What it does not do,

  • it does not remove the List View icon
  • it does not change the query argument in the URL

To remove the List View icon you can do something to the effect of:

add_action('admin_head', function() {

    ?>

    <style type="text/css">
        .view-switch .view-list {
            display: none;
        }
    <style>

    <?php

});

Alternatively, to remove the ?mode=list query argument from the URL, you could:

  • use JavaScript to modify the search property found on windlow.location
  • redirect the user server side using wp_redirect() by inspecting $_GET super global for the mode query variable array key (recommended).

For example you could rewrite the first snippet to:

add_action('admin_init', function() {

    if ( isset( $_GET['mode'] ) && $_GET['mode'] !== 'grid' ) {
        wp_redirect(admin_url('upload.php?mode=grid'));
        exit;
    } else {
        //required by upload.php, handle the case if user just navigates to... 
        //http://www.example.com/wp-admin/upload.php (with no mode query argument)
        $_GET['mode'] = 'grid';
    }

}, 100);

Or alternativey without the need for an else block,

add_action('admin_init', function() {

    if ( strpos(admin_url('upload.php'), $_SERVER['REQUEST_URI']) !== false 
         || (isset($_GET['mode']) && $_GET['mode'] === 'list') ) {
        wp_redirect(admin_url('upload.php?mode=grid'));
        exit;
    } 

}, 100);

...the above snippet ensures that no matter what, either of the following URLs,

...will always be redirected to,

Combined with hiding the List View icon via injecting CSS on the admin_head hook, produces the desired result you are after.

3
  • 1
    Thank you :) I was just wandering aroud a similar solution :)
    – Simone.p
    Mar 16, 2015 at 11:49
  • @Simone.p No problem, I've tested the above and it produces the desired effect you are after.
    – Adam
    Mar 16, 2015 at 11:50
  • 1
    Excellent to the point answer! Mar 9, 2017 at 13:24

Your Answer

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

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