Using WordPress ‘postbox_classes_{$screen_id}_{$box_id}’ PHP filter

The postbox_classes_{$screen_id}_{$box_id} WordPress PHP filter allows you to modify the postbox classes for a specific screen and box ID combination.

Usage

add_filter('postbox_classes_screenID_boxID', 'my_custom_function');
function my_custom_function($classes) {
    // your custom code here
    return $classes;
}

Replace screenID and boxID with the appropriate values.

Parameters

  • $classes (string[]): An array of postbox classes.

More information

See WordPress Developer Resources: postbox_classes_{$screen_id}_{$box_id}

Examples

Add a custom class to postbox

Add a custom class “my-custom-class” to a specific postbox.

add_filter('postbox_classes_post_mymetabox', 'add_custom_class_to_postbox');
function add_custom_class_to_postbox($classes) {
    $classes[] = 'my-custom-class';
    return $classes;
}

Remove a specific class from postbox

Remove the “postbox” class from a specific postbox.

add_filter('postbox_classes_post_mymetabox', 'remove_postbox_class');
function remove_postbox_class($classes) {
    $index = array_search('postbox', $classes);
    if (false !== $index) {
        unset($classes[$index]);
    }
    return $classes;
}

Add multiple custom classes to postbox

Add multiple custom classes “class-one” and “class-two” to a specific postbox.

add_filter('postbox_classes_post_mymetabox', 'add_multiple_custom_classes');
function add_multiple_custom_classes($classes) {
    $classes[] = 'class-one';
    $classes[] = 'class-two';
    return $classes;
}

Replace an existing class with a custom class

Replace the “postbox” class with a custom class “my-custom-postbox” in a specific postbox.

add_filter('postbox_classes_post_mymetabox', 'replace_existing_class');
function replace_existing_class($classes) {
    $index = array_search('postbox', $classes);
    if (false !== $index) {
        $classes[$index] = 'my-custom-postbox';
    }
    return $classes;
}

Clear all classes from a postbox

Remove all existing classes from a specific postbox.

add_filter('postbox_classes_post_mymetabox', 'clear_all_classes');
function clear_all_classes($classes) {
    return [];
}