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.
Just add the class to the markup you are generating in the callback.
Let’s say you want a class for the excerpt box. Then you can do:
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 inadd_meta_box
.DOCS: https://developer.wordpress.org/reference/hooks/postbox_classes_page_id/