[wp-hackers] Closed Metaboxes

rovo89 lists at robv.de
Mon Apr 12 17:50:12 UTC 2010


> Is there any way to have custom metaboxes be closed by default?

Closed metaboxes are handled by the function postbox_classes(), which 
has not filters itself. However, it uses get_user_option(), so you can 
do the following:

function myclosedmetaboxes($result) {
     $closedMetaboxes = array('your_closed_metabox');
     if ( !is_array(result) )
         return $closedMetaboxes;
     else
         return array_merge($result, $closedMetaboxes);
}
add_filter('get_user_option_closedpostboxes_yourposttype', 
'myclosedmetaboxes');

yourposttype is the same as the $page parameter of add_meta_box. The 
array defines the metaboxes which are always closed when the user enters 
the editing page.

If you want to be a bit more user friendly and keep the open/closed 
state of the metaboxes, just return $result instead of the merged array. 
The metabox will then be hidden by default for new users, but if the 
expand them, they will stay expanded. For existing users for who the 
open/closed state was already saved, the metabox will be visible by 
default because it is not in the returned array (which would also be the 
case if they had expanded it themselves). You could query all users who 
have these settings saved and add your metabox to it once to work around 
this.

I hope this helps.
Robert


More information about the wp-hackers mailing list