WordPress: Change meta box location

Recently, I got an interesting support question for Slim SEO about how to change location of a custom meta box in WordPress:

I'm using Slim SEO and Meta Box. When I have Slim SEO activated it shows on top of my custom meta boxes (created by Meta Box). I know that I can just drag those meta boxes, but that's not a solution for new users. Is there a way to change the priority of the Slim SEO meta box to be below my custom meta boxes?

To make the question clear, see this screenshot:

There are 2 custom meta boxes on this screen:

By default Slim SEO puts its meta box to the top of meta box list in the (technically) "normal" location with "high" priority. This means the meta box is always at the top of the list.

When users register custom meta boxes with Meta Box, they can choose which location and priority for them. But as Slim SEO has a high priority, we are not sure if custom meta boxes are at the top or nor. Usually, they're displayed below Slim SEO's one.

So, the question is how to move the Slim SEO meta box to the bottom of the list?

Move a meta box to another location

After doing some researches, I found that it can be done with the following snippet:

add_action( 'add_meta_boxes', function() {
    global $wp_meta_boxes;

    $post_type = 'post';

    // Get Slim SEO meta box.
    $slim_seo_meta_box = $wp_meta_boxes[$post_type]['normal']['high']['slim-seo'];
    unset( $wp_meta_boxes[$post_type]['normal']['high']['slim-seo'] );

    // Move it to 'advanced' location with 'low' priority.
    if ( empty( $wp_meta_boxes[$post_type]['advanced'] ) ) {
        $wp_meta_boxes[$post_type]['advanced'] = [];
    }
    if ( empty( $wp_meta_boxes[$post_type]['advanced']['low'] ) ) {
        $wp_meta_boxes[$post_type]['advanced']['low'] = [];
    }
    $wp_meta_boxes[$post_type]['advanced']['low']['slim-seo'] = $slim_seo_meta_box;
}, 99 );

You can put the snippet in your theme's functions.php file or use a plugin like Code Snippets to insert the code.

What does the snippet do?

The main thing here is that after registering meta boxes, WordPress store them in a global variable $wp_meta_boxes. This variable is an array of all meta boxes, categorized by locations and priorities. All we need to do is find the Slim SEO meta box and move it to another location.

Note that we hook to the add_meta_boxes action with priority 99 to make sure all meta boxes are already registered.

Result

After running the snippet, here is what you get:

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

Your email address will not be published. Required fields are marked *


Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO