How to add a class to meta box

When adding a metabox, i.e.:

add_meta_box( 
    'metabox_id',
    'Metabox Title',
    'my_metabox_callback',
    'page',
    'normal',
    'low', 
    array( 'foo' => $var1, 'bar' => $var2)
);

how do I add a class to it for css styling? I’d like to avoid having to call each id in the style rule for metaboxes that contain elements having the same style.

Related posts

Leave a Reply

2 comments

  1. Let’s say you want a class for the excerpt box. Then you can do:

    add_filter('postbox_classes_post_postexcerpt','add_metabox_classes');
    
    function add_metabox_classes($classes) {
        array_push($classes,'another_class');
        return $classes;
    }
    

    With this method you need to add a filter for every box u need to add a class for. The filter is applied in the function postbox_classes in wp-admin/includes/post.php

    In general the hook is postbox_classes_{$page}_{$id} where $page is the page identifier (e.g. ‘post’ for posts and (presumably) ‘custom-post-type’ for posts of type ‘custom-post-type’). $id refers to the ID of the metabox, automatically assigned for ‘default’ metaboxes or specified in add_meta_box.

    DOCS: https://developer.wordpress.org/reference/hooks/postbox_classes_page_id/