Maggy Mae
Maggy Mae

Reputation: 61

How to change Default Screen Options in Wordpress

I'm looking for a way to change the default screen options in the post editor. I want to hide certain options by default. I am putting together a family recipe site and don't want to overwhelm users with too many options. I don't want to log in as each user and changing their options manually. I've combed through WP core files and theme files and can't find very many references to screen-options. Is it defined somewhere in the database?

Thanks in advance.

Upvotes: 6

Views: 12005

Answers (3)

Matt Keys
Matt Keys

Reputation: 439

A slight modification to Zendka's answer. I just wanted to remove one item from the list, leaving the array otherwise unchanged.

add_filter( 'default_hidden_meta_boxes', 'show_author_metabox', 10, 2 );
function show_author_metabox( $hidden, $screen )
{
    $authorkey = array_search( 'authordiv', $hidden );
    unset( $hidden[ $authorkey ] );

    return $hidden;
}

In my case I was removing the 'authordiv' from the hidden list, swap that out with whatever metabox you want to remove from the hidden meta boxes.

I don't check for the existence of the metabox before unsetting it, because it doesn't produce any PHP notices/errors if there are no results from the array_search.

Upvotes: 2

zendka
zendka

Reputation: 1327

Use the default_hidden_meta_boxes filter

add_filter( 'default_hidden_meta_boxes', 'my_hidden_meta_boxes', 10, 2 );
function my_hidden_meta_boxes( $hidden, $screen ) {
    return array( 'tagsdiv-post_tag', 'tagsdiv', 'postimagediv', 'formatdiv', 'pageparentdiv', ''); // get these from the css class
}

or

add_filter( 'hidden_meta_boxes', 'my_hidden_meta_boxes', 10, 2 );

Bonus: To understand how it works have a look at the core function get_hidden_meta_boxes(). Here's a simplified version:

function get_hidden_meta_boxes( $screen ) {
    $hidden = get_user_option( "metaboxhidden_{$screen->id}" );
    if ( $use_defaults ) {
        $hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen );
    }
    return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults );
}

Upvotes: 8

alpipego
alpipego

Reputation: 3220

The default screen options are saved in wp_usermeta the meta_key is metaboxhidden_post.

I think the easiest way to set default options (or to hide specific boxes) would be to use a plugin like adminimize. I personally use the advanced custom fields plugin for this task (and a lot more).

Upvotes: 3

Related Questions