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.
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.
Share Improve this question edited Mar 16, 2015 at 11:51 Mayeenul Islam 12.9k21 gold badges85 silver badges169 bronze badges asked Mar 16, 2015 at 10:30 Simone.pSimone.p 611 silver badge3 bronze badges1 Answer
Reset to default 11This 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 onwindlow.location
- redirect the user server side using
wp_redirect()
by inspecting$_GET
super global for themode
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,
- http://www.example.com/wp-admin/upload.php
- http://www.example.com/wp-admin/upload.php?mode=list
...will always be redirected to,
- http://www.example.com/wp-admin/upload.php?mode=grid
Combined with hiding the List View icon via injecting CSS on the admin_head
hook, produces the desired result you are after.