• Resolved antoniomartoni

    (@antoniomartoni)


    Hello Everyone,

    I am trying to better understand how sortable metaboxes work in WordPress, particularly for custom post types. By default the metabox are sortable and draggable by the user.

    I am looking for a way to move the “Publish” metabox (#submitdiv) to the end of my custom post type page. How can I interact with the metaboxes to prevent them from being sortable and ensure that the “Publish” metabox appears at the very bottom of the page? Is it handled in javascript or PHP ? Are the user preferences stored in some way ?

    I tried this code : the first remove_meta_box works, but add_meta_box always places the #submitdiv in the same spot, no matter the given values.

    function reorder_meta_boxes() {
    remove_meta_box('submitdiv', 'artisans', 'side');
    add_meta_box( 'submitdiv', __('Save'), 'post_submit_meta_box', 'artisans', 'advanced', 'low' );
    }
    add_action('admin_init', 'reorder_meta_boxes');

    Any documentation or ideas are welcomed,

    Thank you for your help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The meta boxes are generated from PHP, but dragging functionality is implemented with client side scripting –JavaScript or one of its variants. The user’s preferences are saved in user meta. I don’t know much more than that. I assume the preferences are sent to the server via Ajax, so you can learn more about what data is sent where by examining the Ajax request in your browser’s Network developer tool. There will be an action parameter which in part constitutes the name of the Ajax action hook that calls the handler function.

    Action handler functions are stored in the global $wp_filter array. From that you can learn which function is called. The function’s source code will tell you what is saved where.

    You ought to be able to prevent dragging by removing or overriding the element’s drag event handler. Or prevent the preference data from being saved so even if the boxes are moved, the positioning will not persist.

    Thread Starter antoniomartoni

    (@antoniomartoni)

    I found a solution to my problem. I’m not sure this is the appropriate way to solve it, but in case this can help anyone :

    First, I tried to remove and re-insert the metabox (in two different actions) like this :

    function remove_submit_div(){
    remove_meta_box('submitdiv', 'artisans', 'side');
    }
    add_action('admin_menu', 'remove_submit_div');


    function reinsert_submitdiv_meta_box()
    {
    add_meta_box(
    'submitdiv',
    __('Sauver'),
    'post_submit_meta_box',
    'artisans',
    'normal',
    'low'
    );
    }
    add_action('do_meta_boxes', 'reinsert_submitdiv_meta_box');

    This works, but can get replaced by the user preferences, and does not allow to have a fine control on the metabox order. So I prevented the metaboxes order from being saved as mentionned in this solution

    Then, to have a more precise control on the exact order I am expecting, I update the user_meta to make sure my order does not get overwritten by the previous user preferences :

    function set_user_metabox_order($user_id){
    $new_order = [
    'acf_after_title' => '',
    'side' => 'postimagediv,icl_div',
    'normal' => 'acf-group_67..,a_custom_metabox,acf group_67..,acf-group_67..,submitdiv,revisionsdiv',
    'advanced' => ...whatever
    ];
    update_user_meta($user_id, 'meta-box-order_artisans', $new_order);


    }
    set_user_metabox_order(get_current_user_id());

    This is not the ideal solution but does the trick for me. I would obviously welcome a better solution.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.