Is there a way to set default visible columns in posts list, for all users?
2 Answers
Since the question was about columns and not meta boxes and I needed this solution, Ioannis’ reply got me on the right track.
The filter hook in question is default_hidden_columns
.
This is the solution I ended up with to set my ad_shortcode
column to be hidden by default. You should know that this is just the default. As soon as the page was visited, the default is no longer be used. Look for a meta key that includes columnshidden
in wp_usermeta
and remove it when testing.
add_filter( 'default_hidden_columns', 'hide_ad_list_columns', 10, 2 );
function hide_ad_list_columns( $hidden, $screen ) {
// "edit-advanced_ads" needs to be adjusted to your own screen ID, this one is for my "advanced_ads" post type
if( isset( $screen->id ) && 'edit-advanced_ads' === $screen->id ){
$hidden[] = 'ad_shortcode';
}
return $hidden;
}
-
Thank you for posting this! Every other solution I have found was about hiding meta boxes which I needed to keep. This one works perfectly to just hide the columns but keep them available (after I modified it for my usage on the normal posts screen).– jsmodCommented Oct 22, 2020 at 11:56
To change the defaults, you simply need to hook into the default_hidden_meta_boxes filter and supply your own PHP array listing the meta boxes you’d like hidden by default. In the example below, I hide the author meta box and the revisions meta box. This way, they’re hidden for users unless they’ve decided to enable them in Screen Options.
<?php
/**
* vpm_default_hidden_meta_boxes
*/
function vpm_default_hidden_meta_boxes( $hidden, $screen ) {
// Grab the current post type
$post_type = $screen->post_type;
// If we're on a 'post'...
if ( $post_type == 'post' ) {
// Define which meta boxes we wish to hide
$hidden = array(
'authordiv',
'revisionsdiv',
);
// Pass our new defaults onto WordPress
return $hidden;
}
// If we are not on a 'post', pass the
// original defaults, as defined by WordPress
return $hidden;
}
add_action( 'default_hidden_meta_boxes', 'vpm_default_hidden_meta_boxes', 10, 2 );
Also take a look at
How to set default screen options?
and
https://www.vanpattenmedia.com/2014/code-snippet-hide-post-meta-boxes-wordpress