Using WordPress ‘postbox_classes()’ PHP function

The postbox_classes() WordPress PHP function returns the list of classes to be used by a meta box.

Usage

postbox_classes( string $box_id, string $screen_id );

Parameters

  • $box_id (string) – Required. The meta box ID (used in the ‘id’ attribute for the meta box).
  • $screen_id (string) – Required. The screen on which the meta box is shown.

More information

See WordPress Developer Resources: postbox_classes()

Examples

Add a custom class to a meta box

// Hook into 'postbox_classes' filter
add_filter( 'postbox_classes_post_my_custom_meta_box', 'add_custom_class_to_meta_box' );

function add_custom_class_to_meta_box( $classes ) {
    // Add custom class 'my-custom-class' to the meta box
    $classes[] = 'my-custom-class';
    return $classes;
}

Conditionally add a class to a meta box

// Hook into 'postbox_classes' filter
add_filter( 'postbox_classes_post_another_custom_meta_box', 'conditionally_add_class_to_meta_box' );

function conditionally_add_class_to_meta_box( $classes ) {
    // Add class 'hide' if the condition is met
    if ( some_condition_is_met() ) {
        $classes[] = 'hide';
    }
    return $classes;
}

Remove a class from a meta box

// Hook into 'postbox_classes' filter
add_filter( 'postbox_classes_post_remove_class_meta_box', 'remove_class_from_meta_box' );

function remove_class_from_meta_box( $classes ) {
    // Remove 'closed' class from the meta box
    if ( ( $key = array_search( 'closed', $classes ) ) !== false ) {
        unset( $classes[$key] );
    }
    return $classes;
}

Add multiple classes to a meta box

// Hook into 'postbox_classes' filter
add_filter( 'postbox_classes_post_multiple_classes_meta_box', 'add_multiple_classes_to_meta_box' );

function add_multiple_classes_to_meta_box( $classes ) {
    // Add multiple custom classes to the meta box
    $classes[] = 'custom-class-1';
    $classes[] = 'custom-class-2';
    return $classes;
}

Replace a class on a meta box

// Hook into 'postbox_classes' filter
add_filter( 'postbox_classes_post_replace_class_meta_box', 'replace_class_on_meta_box' );

function replace_class_on_meta_box( $classes ) {
    // Replace 'closed' class with 'open' class
    if ( ( $key = array_search( 'closed', $classes ) ) !== false ) {
        $classes[$key] = 'open';
    }
    return $classes;
}