Using WordPress ‘postbox_classes_{$page}_{$id}’ PHP filter

The postbox_classes_{$page}_{$id} WordPress PHP filter lets you modify the postbox classes for a specific screen and screen ID combination.

Usage

add_filter('postbox_classes_{your_page}_{your_id}', 'your_custom_function');
function your_custom_function($classes) {
  // Your custom code here
  return $classes;
}

Parameters

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

More information

See WordPress Developer Resources: postbox_classes_{$page}_{$id}

Examples

Add a custom class to a specific postbox

Add the ‘my-custom-class’ to a postbox with the screen ‘dashboard’ and ID ‘example_id’:

add_filter('postbox_classes_dashboard_example_id', 'add_custom_class_to_postbox');

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

Remove a specific class from a postbox

Remove the ‘hide-if-js’ class from a postbox with the screen ‘post’ and ID ‘my_postbox_id’:

add_filter('postbox_classes_post_my_postbox_id', 'remove_class_from_postbox');

function remove_class_from_postbox($classes) {
  $classes = array_diff($classes, array('hide-if-js'));
  return $classes;
}

Add a prefix to all classes in a postbox

Add a prefix ‘myprefix-‘ to all classes of a postbox with the screen ‘post’ and ID ‘sample_id’:

add_filter('postbox_classes_post_sample_id', 'prefix_all_classes_in_postbox');

function prefix_all_classes_in_postbox($classes) {
  foreach ($classes as &$class) {
    $class = 'myprefix-' . $class;
  }
  return $classes;
}

Remove all classes from a postbox

Remove all classes from a postbox with the screen ‘dashboard’ and ID ‘remove_classes_id’:

add_filter('postbox_classes_dashboard_remove_classes_id', 'remove_all_classes_from_postbox');

function remove_all_classes_from_postbox($classes) {
  return array();
}

Conditionally add a class to a postbox

Add the ‘conditional-class’ to a postbox with the screen ‘post’ and ID ‘conditional_id’ only if the current user has a specific capability:

add_filter('postbox_classes_post_conditional_id', 'conditionally_add_class_to_postbox');

function conditionally_add_class_to_postbox($classes) {
  if (current_user_can('manage_options')) {
    $classes[] = 'conditional-class';
  }
  return $classes;
}