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.
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,
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:
search
property found on windlow.location
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.