How to re-order Meta-boxes in wordpress

  • admin
  • Tag 1
    Tag 2
    Tag 3
    Tag 4

    I recently had the privilege of working on a rather big real-estate site. The project required an extensive amount of back-end work, to make the editor as intuitive and easy to use as possible. This requirement came from the fact that some of the users of the site may not be very tech savvy. So the biggest challenge was in cleaning up the post editor in wordpress, because let’s be honest; it’s really cluttered with a lot of useless fields. Another major aspect of the project, was that it required a rather large amount of custom meta boxes for property data. Rather than just have the meta boxes strewn all over the place, I wanted to create a “flow” that made sense. This meant having full control of which meta box went where. Since I didn’t have control over some of the meta boxes (Some were registered via the theme we used. Some where registered via Advanced custom fields, and yet more where registered via my own custom code) this proved to be a rather cumbersome task, as I could not for the life of me get all the fields lined up right.

    So after much hair pulling, I did what any self respecting developer would do: I came up with my own custom solution. First task was finding the proper hook to utilize. After a bit of researching, I found we needed to use this:

     
    
    add_action( 'edit_form_after_title', 'resort_my_meta_boxes_filter' ));
    
    

    It turns out, that the ‘edit_form_after_title’ hook is fired right after the initial post editors title field is output, but before anything else is displayed. Now that I had the correct hook, I set about writing the function to sort it all out.

    First off, we write the function to call our sorting function as referenced in the action hook above.

    
    function resort_my_meta_boxes_filter() {
    
         resort_meta_boxes ('noo_property');
    
    }
    

    This will call our sorting function. Note the argument, this is the post type you want the sorting to occur on. In our case, it was our registered CPT ‘noo_property’.

    Now for the sorting function.

        function resort_meta_boxes ($custom_post_name='post') {
                    
            global $wp_meta_boxes;
    
            $resorted = array(
                'Rental Description'=>'7',
                'Property Details'=>'1',
                'Property Media'=>'2',
                'Place in Map'=>'3',
                'Contact Information'=>'5',
                'Property Notes'=>'4',
                'Extra Property Info'=>'6',
                'Dates Booked'=>'0'
                 );
            
            foreach($wp_meta_boxes[$custom_post_name]['normal']['high'] as $k=>$v) {
                
                if (key_exists($v['title'], $resorted)) {
                    
                    $key =  $resorted[$v['title']];
                    
                    $array[$key] = $wp_meta_boxes[$custom_post_name]['normal']['high'][$k];    
                     
                } else {
                        
                    $after_array[$k] = $v;    
                    
                }
                             
            }
            
            ksort($array);
            
            $wp_meta_boxes['noo_property']['normal']['high'] = array_merge($array, $after_array );
            
        }
    

    What this does, is take all of our meta boxes registered under the “high” priority, sorts them out by our criteria, and then re-creates the $wp_meta_boxes array. Note that in this instance, I wanted to sort by the titles of the meta boxes. I built our “sorting” array with the order I wanted them sorted in, and voila. Anything not in our sorting array just gets appended to the end of the array, which means if it’s not in the sort, it gets put in after everything else.

    Leave a comment

  • (required)