改编自此Q&A,以下将强制自定义元框进入侧栏中的第二个位置。
检查评论并注意admin_init
.
它仅在用户自己没有重新排列位置时才有效。注册新用户时,为他设置位置,作为钩子admin_init
并附user_register
加到相同的回调函数。
// This fires at **every** page load, a better hook must be found
add_action( 'admin_init', 'set_user_metaboxes_so_14183498' );
// This fires when a new user is created
add_action( 'user_register', 'set_user_metaboxes_so_14183498' );
function set_user_metaboxes_so_14183498( $user_id = null )
{
// This is the metakey we will need to update
$meta_key = 'meta-box-order_post';
// So this can be used without hooking into user_register
if( !$user_id )
$user_id = get_current_user_id();
// Set the default order if it has not been set yet by the user.
// These are WP handles, PLUS our custom meta box handle
if ( ! get_user_meta( $user_id, $meta_key, true ) )
{
$meta_value = array(
'side' => 'submitdiv,custom_meta_box,formatdiv,postimagediv',
'normal' => 'postcustom,commentsdiv,slugdiv,revisionsdiv',
'advanced' => '',
);
update_user_meta( $user_id, $meta_key, $meta_value );
}
}