WordPress restore default post meta boxes: post image, custom fields, excerpt, author, revisions

One of the most frustrating things I’ve encountered while working with WordPress came a few versions ago when they decided it was a good idea to ‘clean up’ the editing interface by hiding most of the post meta box options.

In the version 3.1 release notes, they write:

“Streamlined Writing Interface – new users of WordPress will find the write screen much less cluttered than before, as more of the options are hidden by default. You can click on Screen Options in the top right to bring them back.”

Here’s what the ‘Screen Options’ looks like, when expanded:

Toggling each check box will customize your preferences for that particular user account, but when you’re developing a WordPress theme for hundreds of users (like I currently am in the middle of), consistency and documentation is important. I need to know that every user is going to start with the same screen options out of the box.

Bring them back

While there seems to be good WordPress documentation on how to remove meta boxes or adding new custom meta boxes, the simple act of restoring what was once there has been a challenge.

Here’s the simple call to add a meta box:
add_meta_box( $id, $title, $callback, $page, $context, $priority, $callback_args );

To use it, you need to know the ID of the div you want to show ($id) and the callback function you want to run ($callback). For those writing custom meta boxes, these two things are easy to find because you create them.

But if you’re restoring the basic WordPress editor options, you have to track down what they already are.

After researching and testing, here are the $id and $callback names needed for each:


formatdiv post_format_meta_box
categorydiv post_categories_meta_box
tagsdiv-post_tag post_tags_meta_box
postimagediv post_thumbnail_meta_box
postcustom post_custom_meta_box
authordiv post_author_meta_box
postexcerpt post_excerpt_meta_box
revisionsdiv post_revisions_meta_box
commentstatusdiv post_comment_status_meta_box
commentsdiv post_comment_meta_box
trackbacksdiv post_trackback_meta_box
slugdiv post_slug_meta_box

So to pull the whole thing off, add this to your functions.php file. You don’t need them all though, only the lines that you specifically want to add. Also, the user’s custom preference still takes precedent. So as soon as they start showing/hiding screen options, WordPress goes by that alone. It’s written into core with no option to filter (template.php line 1050).


function custom_add_default_metaboxes() {
add_meta_box('formatdiv', __('Formats'), 'post_format_meta_box', 'post', 'advanced', 'core');
add_meta_box('categorydiv', __('Categories'), 'post_categories_meta_box', 'post', 'advanced', 'core');
add_meta_box('tagsdiv-post_tag', __('Tags'), 'post_tags_meta_box', 'post', 'advanced', 'core');
add_meta_box('postimagediv', __('Post Image'), 'post_thumbnail_meta_box', 'post', 'advanced', 'core');
add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', 'post', 'advanced', 'core');
add_meta_box('authordiv', __('Post Author'), 'post_author_meta_box', 'post', 'advanced', 'core');
add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'post', 'advanced', 'core');
add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', 'post', 'advanced', 'core');
add_meta_box('commentstatusdiv', __('Comments Status'), 'post_comment_status_meta_box', 'post', 'advanced', 'core');
add_meta_box('commentsdiv', __('Comments'), 'post_comment_meta_box', 'post', 'advanced', 'core');
add_meta_box('trackbacksdiv', __('Trackbacks'), 'post_trackback_meta_box', 'post', 'advanced', 'core');
add_meta_box('slugdiv', __('Slug'), 'post_slug_meta_box', 'post', 'advanced', 'core');
}
add_action( 'admin_menu', 'custom_add_default_metaboxes' );

UPDATE: Well, it looks like none of that above stuff really works once you customize your user profile screen options.

The code in tempalte.php clearly sets the $hidden variable to what is stored in the DB for that user and then if there’s nothing in the DB, it runs the default hidden array and checks for a filter after that. So running filters won’t help if you’ve ever set your own screen options at any time. Even if you restored them to ‘default’. Pretty lame.

So if you want to adjust the default setting for new users, you have to use this code found here: http://wordpress.org/support/topic/extra-fields-dissapeared-in-new-post?replies=7

// Change what's hidden by default
add_filter('default_hidden_meta_boxes', 'be_hidden_meta_boxes', 10, 2);
function be_hidden_meta_boxes($hidden, $screen) {
if ( 'post' == $screen->base || 'page' == $screen->base )
$hidden = array('slugdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv', 'revisionsdiv');
// removed 'postcustom',
return $hidden;
}

And just customize that hidden array with the $ID that you wish to not show. So $hidden = array(); will show everything. And again, this only works for users who haven’t ever customized their options. Once you do, it’s too late!

Advertisements