I am using an OOP approach to doing all the work in creating the CPT and its meta boxes. For admin styling purposes, I want to do some heavier styling for a specific custom post type. My object method creates the following HTML results:

<div id="client_info" class="postbox ">
    <button type="button" class="handlediv" aria-expanded="true"><span class="screen-reader-text">Toggle panel: Client Info</span><span class="toggle-indicator" aria-hidden="true"></span></button>
    <h2 class='hndle'><span>Client Info</span></h2>
    <div class="inside">
        <label for="client_info_client_first_name">Client First Name</label>
        <input type="text" name="custom_meta[client_info_client_first_name]" id="client_info_client_first_name" value="" />
        <label for="client_info_client_last_name">Client Last Name</label>
        <input type="text" name="custom_meta[client_info_client_last_name]" id="client_info_client_last_name" value="" />
        <label for="client_info_client_age">Client Age</label>
        <input type="number" name="custom_meta[client_info_client_age]" id="client_info_client_age" value="" />

What I want to do is add a custom class to the <div> element for each box that is the same so that the class attribute would be like: class="postbox my-class"

I have tried using add_filter() in order to do this but the problem is the only hook I can find relevent is the postbox_classes_{$page}_{$id} which I cannot figure out how to make dynamic as I want my object's method to handle registering the filter. I have tried using concatenation with . and HereDoc style:

function meta_box_add_class( $classes ) {
    array_push( $classes, 'jldc-fitcase-meta' );
    return $classes;
add_filter( 'postbox_classes_custom-post-type_' . $box_id, 'meta_box_add_class' );
add_filter( 'postbox_classes_custom-post-type_{$box_id}', 'meta_box_add_class' );

Is there a way to add this hook dynamically as each metabox is created?

  • 1
    fyi, the body tag has classes that let you target specific post type admin screens. – Milo Dec 18 '17 at 3:13
  • Thanks. I didn't even think to look at that. But I am still curious if it is possible to use hooks like I was originally attempting. – Cedon Dec 18 '17 at 10:30
  • I actually solved it. I cannot do it directly with the add_action() or add_filter() statement, but rather I can create a variable and and then do it. Your solution worked by the problem is the CSS would effect every meta box, including the default ones. – Cedon Dec 19 '17 at 1:23

After doing some playing around, I discovered this can actually be accomplished by defining the name of the hook I want to add the filter to via a variable.

So if my $post_type_name is portfolio and my $box_id is piece_info, I can make a filter as follows:

function add_meta_box_class( $classes ) {
   array_push( $classes, 'custom-class');
   return $classes;
$filter_name = 'postbox_classes_' . $post_type_name . '_' . $box_id;
add_filter( $filter_name , 'add_meta_box_class' );

This will result in a filter being created using the postbox_classes_{$page}_{$id} hook as postbox_classes_portfolio_piece_info.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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