admin管理员组文章数量:1421000
I have a custom post meta box that I want to put in the right sidebar. When I set the priority to high
it appears in the number 1 spot in sidebar above everything. If I set to core
it appear at the bottom of right sidebar.
My desired position is in the number 2 spot in the right sidebar. Right below the publish metabox.
Is this possibble?
I have a custom post meta box that I want to put in the right sidebar. When I set the priority to high
it appears in the number 1 spot in sidebar above everything. If I set to core
it appear at the bottom of right sidebar.
My desired position is in the number 2 spot in the right sidebar. Right below the publish metabox.
Is this possibble?
Share Improve this question asked Dec 27, 2015 at 2:46 JasonDavisJasonDavis 1,6906 gold badges36 silver badges57 bronze badges 1- There is no direct way in WP core to exactly order it below publish metabox. However, you can change the order by drag-dropping it as intended after registering the metabox. So if you reorder it to be below publish metabox then that order is saved on future visits too. – Prasad Nevase Commented Dec 27, 2015 at 9:39
2 Answers
Reset to default 7Still if its something like client requirement then below is somewhat hack sort of solution. Add this code to your add_meta_box()
function. For the sake of understanding, below I have provided complete function but you will need to use selective code :-) Do let me know how it goes.
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'testdiv',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen,
'side'
);
}
/* Get the user details to find user id for whom this order should be shown. Ideally, I believe it will be admin user. Make sure you change the email id*/
$user = get_user_by( 'email', '[email protected]' );
$order = get_user_option("meta-box-order_post", $user->ID);
$current_order = array();
$current_order = explode(",",$order['side']);
for($i=0 ; $i <= count ($current_order) ; $i++){
$temp = $current_order[$i];
if ( $current_order[$i] == 'testdiv' && $i != 1) {
$current_order[$i] = $current_order[1];
$current_order[1] = $temp;
}
}
$order['side'] = implode(",",$current_order);
update_user_option($user->ID, "meta-box-order_page", $order, true);
update_user_option($user->ID, "meta-box-order_post", $order, true);
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box', 2 );
After some experimentation, I have come up with this method which may be a tad more robust:
add_action('add_meta_boxes', function () {
$screens = array('post', 'page');
foreach ($screens as $screen) {
add_meta_box("metabox_name", "Meta Box Title", "my_metabox_callback", $screen, "side", "high");
// Force the box to display below the 'Publish' metabox
$user = wp_get_current_user();
$order = get_user_option("meta-box-order_".$screen, $user->ID);
if(strpos($order['side'], "metabox_name") !== false) {
$order = str_replace('metabox_name,', '', $order['side']);
}
if(strpos($order['side'], "submitdiv") === false) {
$order['side'] = 'submitdiv,' . $order['side'];
}
if(substr($order['side'], -1) == ",") {
$order['side'] = substr($order['side'], 0, -1);
}
$current_order = array();
$current_order = explode(",", $order['side']);
// Add this metabox to the order array
$key = array_search('submitdiv', $current_order, true);
if($key !== false) {
$new_order = array_merge(
array_slice($current_order, 0, $key+1),
array("metabox_name")
);
if(count($current_order) > $key) {
$new_order = array_merge(
$new_order,
array_slice($current_order, $key+1)
);
}
$order['side'] = implode(",", $new_order);
update_user_option($user->ID, "meta-box-order_".$screen, $order, true);
}
}
});
This doesn't feel very elegant to me, but it does work in a variety of situations where the earlier answer fails (e.g. new users).
本文标签: Position right sidebar metabox right below the publish metabox
版权声明:本文标题:Position right sidebar metabox right below the publish metabox? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745338356a2654147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论