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 the 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 collapsed 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.
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 haveclosed
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:
$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:
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.
Another way to do this is to use the hook in get_user_option:
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.
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..