Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created some custom metaboxes for my post write screen. Is there any way to make some of them display collapsed by default?

Just in case I'm not using the correct terms or not being clear in my question I will elaborate:

The metaboxes on the post write screen have a toggle switch in the upper right hand corner of the box. If you mouse over this area, it shows a little downward arrow and the words "Click to toggle". When you click it, the metabox collapses, hiding the contents and only showing the metabox's title bar. I am not talking about the Screen Options which make the entire metaboxes disappear (only on the given account). I just want certain metaboxes to display in the collapsed mode by default for all WP users.

UPDATE:

After reading in Hakre's answer that they toggle state is stored for the user I wanted to add some clarification on how I'd like it to work. I'm not sure if this is possible but I'd like the metaboxes to default to collapased when the post is new, then as the user expands them they should expand, however I want those settings to stay local to the specific post, so when they create a new post all of the metaboxes are collapsed.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

To display a metabox collapsed or closed by default, it is good to know that adding closed to it's class attribute will display it closed. All meta-boxes main divs that have closed in their classname, are displayed in the closed form.

When the arrow is clicked it will be removed or added (toggled). This is done interactively on the post editor page. Manually adding the closed classname does only work if you do that in your browser with a developer tool like firebug or similar.

As Wordpress normally tracks the status of the toggle state of each metabox, you should take care to only initially set the toggle state to close the first time a specific user is visiting the editor or is added to users.

The data which of those metaboxes are closed is stored inside the users options, each time you toggle a box, there will be an ajax request that is storing it:

update_user_option($user->ID, "closedpostboxes_$page", $closed, true);

$closed is a comma-seperated list of all metaboxes IDs that are closed. $page is set to "post" for the post editor (the one you asked for). So when a new user is created (or the plugin is installed for all existing users), you can extend that setting with the ID of your metabox. This should do the job.

The only thing you need to know is the ID of your metabox, but I'm pretty sure you know that one already. If not, check your code where you register the metabox.

Additionally, next to the ones that are closed, you can also set those which are hidden btw, which would correspond to the checkboxes. It's the hiddenpostboxes_$page option then.

Update

Some code snipped on how to add the option:

function collapseBoxForUser($userId, $page, $boxId) {
    $optionName = "closedpostboxes_$page";
    $close = get_user_option($optionName, $userId);
    $closeIds = explode(',', $close);
    $closeIds[] = $boxId;
    $closeIds = array_unique($clodeIds); // remove duplicate Ids
    $close = implode(',', $closeIds);
    update_user_option($userId, $optionName, $close);
}

just call that function with the proper values and it will insert your boxId into the value. Next time the editor is loading that value for the screen (e.g. via an ajax request), it should display that box closed.

share|improve this answer
    
I tried adding 'class' => "closed", into the array that sets up the metaboxes but they still default to expanded and the class is set to "postbox" although I have no idea where that is set. Also, I have added an update to my question. –  matt Nov 21 '10 at 20:17
    
@matt - that was to explain how that works on the page when all scripts are working etc. - so informative not as the solution. Set the option instead for the user, the rest should just work for you. –  hakre Nov 21 '10 at 20:21
    
So just for clarification I am going to put "update_user_option($user->ID, "closedpostboxes_$page", $closed, true);" with the code to create my metabox. Do I need to replace something with the metabox ID? –  matt Nov 21 '10 at 20:27
    
@matt: I added some code-fragment that should make it more clear. Do you know where you find your metaboxID? If you look into the HTML source, you should be able to identify it for your box. Otherwise add the line of code where you register your metabox. –  hakre Nov 21 '10 at 20:31
    
@matt: and keep in mind to only call this once per user (not once everytime you create the metabox) otherwise the box will be displayed closed all the time. –  hakre Nov 21 '10 at 20:33

Another way to do this is to use the hook in get_user_option:

function closed_meta_boxes( $closed ) {
    if ( false === $closed )
        $closed = array( 'submitdiv', 'postcustom', 'anothermetaboxid' );

    return $closed;
}
add_filter( 'get_user_option_closedpostboxes_{post_type_slug}', 'closed_meta_boxes' );
share|improve this answer
    
This looks promising, but can someone provide me a link to where I can find more information on the 'get_user_option_closedpostboxes_{post_type_slug}' filter? And/or, let us know what the 'anothermetaboxid' string is for? Thanks.:) –  user1617737 Jan 1 at 19:20
    
Also, you may consider changing the "if (false === $closed )" line to "if (empty($closed))" because, on my tests, $closed is an empty array. –  user1617737 Jan 1 at 19:30

You could hook onto the user register hook and apply the postbox state changes then, it would simply run whenever a new user is created.

add_action( 'user_register', 'set_user_closedboxes' );
function set_user_closedboxes( $uid ) {
    update_user_meta( $uid, 'closedpostboxes_{type}',array('submitdiv','postcustom'));
}

Where {type} should be your applicable post type, and the array should hold IDs of the boxes to hide(submitdiv and postcustom being two examples).

You'll need to write a query if you wish to update existing users, but the above should cover newly created/registered users..

Code is untested, but i see no reason it shouldn't work, report back if any problems.

Hope that helps..

share|improve this answer
    
Please join chat. –  hakre Nov 22 '10 at 1:02
    
Seems i missed the chat unfortunately... any comments on my above suggestion for hooking onto the new user registration/creation? –  t31os Nov 22 '10 at 8:44
1  
works great for me. Don't forget to replace {type} with the slug of the post type you're addressing (post, page, custom post type, etc.) –  somatic Sep 21 '11 at 21:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.