2

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?

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. Dec 27, 2015 at 9:39

2 Answers 2

7

Still 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', 'xyz@example.com' ); 
    $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 );
0
0

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.