Remove MetaBoxes form Specific Pages

Here is a very simple and robust way to remove the meta boxes from specific pages. It will remove the specified meta boxes from any page slugs in the array of slugs.

Code to Remove Metaboxes from Specific Pages

add_action('do_meta_boxes', function() {
    $post_id = $_GET['post'] ?? ($_POST ?? false);
    global $edit_post_id;

    // just call it once - don't repeat yo'self!
    if ($edit_post_id !== $post_id) {
        global $post;
        $edit_post_id = $post_id;

        // array of pages that will exclude the metabox(es)
        $exclude_on_pages = [
            'home'
        ];

        // remove the metabox(es) from the specific pages
        if (in_array($post->post_name, $exclude_on_pages)) {
            remove_meta_box('postimagediv', 'page', 'side');
        }
    }
});

Conclusion

Plonk this in a plugin or your theme’s codebase and you’re good to go. If you have any questions or would like help with WordPress related challenges, call us and we’ll gladly help.

Reply