I was wondering : You are an administrator : when you re-order the layout of your meta-box, in a page, or in a post, this layout is saved with your user profil : for example you drag the category box before the publish box…
Is it possible to save this layout for other users (editors, authors..) ?
Is it possible to organize your layout for every kind of users, without logging with their account ?
Thanks for you help !
You could setup a different default layout by hooking into
do_meta_boxes
, take a look here:Or you could use the global variable
$wp_meta_boxes
to do so:Although the last two links are about dashboard widgets, there is little to none difference in how it is done for meta boxes on edit screens. Doing it with either method would be of course setting this up programmatically. You’d still have to disable the dragging and dropping I guess.
This is a rough outline but something like this should work:
administrator or a user created for this purpose.
if boxes have been moved around.
get_user_option_{$option}
.Put that together:
I have serious misgivings about doing this at all. It could well cause confusion for your users. Even if that sketch of filter works, or you can make it work, I’d consider disabling the metabox dragging for any but the “master” user.
You will have to do something very similar for other pages with meta boxes that you want to hijack.
You can use the sorting behavior of ordering the metaboxes.
add_meta_box( $id, $title, $callback, $post_type, $context,
$priority, $callback_args );
There are priorities (
$priority
) to display. My Idea is to give all of them same priority but keep their id’s in sorted manner.For example: If I want *c_meta_box* to be displayed before *b_meta_box* then i will register my meta boxes something like this.
Notice: How i have changed the id’s of the boxes and added a_ and b_ to sort them. You can add numbers too. Giving all of them same
$priority
but sorted id’s.I know this is not the perfect solution but it works in this scenario.
For trouble shooting you can use this code to see look at the
global $wp_meta_boxes
like this:Further References: Widget API Codex
NOTE: After making changes on existing metaboxes hit Publish Button once to save the meta box’s changed id’s.
I agree that this is sort of bad practice as you’re taking control out of users’ hands and may confuse them, but I also understand that many users don’t use this feature and/or are hopelessly bad with technology and end up screwing up the interface for themselves (for instance by collapsing meta boxes… different usermeta option for that but identical idea). We also know that hardcoding this stuff is bad practice but sometimes you really just need to get that shit done:
You can find out what values to use here by peeking into your database or
error_log(print_r($order,true))
or whatever.A MUCH better way would be to get these values from another account, like your admin account, which you can then use to control and modify the meta box order. @s_ha_dum’s answer goes for exactly this except that you get an infinite loop in it, as every call to
get_user_option()
goes through your filter, which in turn callsget_user_option()
, etc… You need to have a base case, which checks if the requested user is 1 then actually return the user option. You can’t simply checkget_current_user_id()
though because that will still return the current user viewing the page, even if you’re callingget_user_option('meta-box-order_post', 1)
. I’m sure there’s a way but briefly looking I couldn’t figure out how to avoid this infinite loop.Actually one way around it would be to directly query
$wpdb
for this value instead of using the WordPress functionget_user_option()
.EDIT: With the bug fixed, @s_ha_dum’s answer is better, as it doesn’t hard-code the order but lets a designated user control it: https://wordpress.stackexchange.com/a/124337/41534