WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When adding a metabox, i.e.:

    'Metabox Title',
    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.

share|improve this question
up vote 1 down vote accepted

Just add the class to the markup you are generating in the callback.

share|improve this answer
simple, as there's no built-in way to do this yet. i'll just wrap my metabox content in a classd div. thanks, @KDM – Ana Ban Apr 22 '12 at 3:01

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


function add_metabox_classes($classes) {
    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.

share|improve this answer
this is awesome. but i'd have to admit it'll be much simpler if i just wrap my metabox output in a classd div. thanks so much for the answer, though @offroff you rock. – Ana Ban Apr 22 '12 at 3:00
+1. Just added the general hook. I've not look closely at the source code, but I believe the $page variable, at least for post-edit screens, is the post type. – Stephen Harris Apr 22 '12 at 11:19
Yup can confirm when working on a custom post type that you need to supply the post type in place of {$page} - postbox_classes_posttype_metaboxid – acSlater Apr 11 '15 at 16:53

Be more efficient to code it this way in this case rather than use an array_push


function add_excerpt_metabox_custom_class( $classes ) {
   $classes[] = 'custom-class';
    return $classes;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.