I’d love to be able to hide meta boxes using screen options, instead of removing them or restricting them to user roles, the goal is to just “uncheck” the meta box for the user.
I see how this would be tricky since any code that would make a DB change for a user wouldn’t be allowed to run every time they hit the page because it would just reset. But smarter people than I code the core, so maybe there’s a way. And if there is, I’m dying to know.
Any ideas?
You are referring to the metaboxes on the admin post screen right?
For that you don’t need a plugin, just drop the following into your functions.php file.
Basically what is happening is that for the currently logged in user, you are changing some saved meta_values in the wp_usermeta table.
There are two ways to use this function, you can either hook into ‘user_register’ or you can hook into ‘admin_init’.
The advantage of using ‘user_register’ is that this function will only fire when a new user is registered (thus lower overhead). However it will not work for users that already exist.
If you want it to work for users that already exist, then hook into ‘admin_init’. The disadvantage of course is that now this function fires every time a user goes to the admin page.
There is
default_hidden_meta_boxes
filter inget_hidden_meta_boxes()
that allows to modify which are not shown as long as user hadn’t modified his preferences in screen options.I have code comments about three solutions already posted, but also a general comment
They present a suboptimal choice: either override the user’s Screen Options checkbox choice; or respect it but have your code ignored if the user has ever changed the checkboxes, even for other meta_boxes, or before your code was in WP. Seems the respectful choice is only useful if all your users are new. The best solution would be to set a custom user option to determine if your specific meta_box defaults have been changed by the user, and respect that. No, I haven’t written that code! Should be easy… 🙂
The three solutions posted:
1. WraithKenny’s
hidden_meta_boxes
solution is the one that overrides the user_option. Note it covers all post types (‘post’, ‘page’, ‘link’, ‘attachment’, and any custom post types). That’s fine unless you want it to be specific. You specified the post_type’s in youradd_meta_box()
calls. You can try to match those, or just wing it since the ones that don’t match will be ignored. If you want to know the post_type in the filter, you can use the extrascreen
parameter:2. As Rarst says,
default_hidden_meta_boxes
respects the user_option. As withhidden_meta_boxes
, you can use the $screen parameter to distinguish post_types.3. Drebabels’s set_user_metaboxes() function also respects the user_option. Note it is hard-coded for the ‘post’ edit screen. To handle the ‘page’ edit screen, and other post_types, wrap the code in this loop:
Yeah,
get_user_meta
should beget_user_option
. For single-site it doesn’t matter, and even for multi-site it probably doesn’t. See wp-admin/includes/ajax-actions.php for why:update_user_option
has the ‘true’ global parameter.There is also
hidden_meta_boxes
filter that would “uncheck” the metabox (making it hidden) on every page load (but wouldn’t have to update the database at all). If you just want it off for new users until they check the box themselves, use Rarst’s answer.