Skip to content

Instantly share code, notes, and snippets.

@slushman
Last active August 29, 2015 14:19
Show Gist options
  • Save slushman/07e68d4008bff9d20b92 to your computer and use it in GitHub Desktop.
Save slushman/07e68d4008bff9d20b92 to your computer and use it in GitHub Desktop.
Add a Class to a WordPress Metabox
/**
* The simplistic way to add a custom class to a specific metabox
*
* @param array $classes The current classes on the metabox
* @return array The modified classes on the metabox
*/
function add_metabox_classes( $classes = array() ) {
return array_push( $classes, sanitize_html_class( 'my-custom-class' ) );
} // add_metabox_classes()
/**
* {post_type_name} The name of the post type
* {metabox_id} The ID attribute of the metabox
*/
add_filter( 'postbox_classes_{post_type_name}_{metabox_id}, 'add_metabox_classes' );
/**
* The more thorough way to add a custom class to a specific metabox
*
* Uses the same add_filter call, but its easier to add additional classes
* and it checks if new class is already there.
*
* @param array $classes The current classes on the metabox
* @return array The modified classes on the metabox
*/
function add_class( $classes = array() ) {
$add_classes = array( 'new_class1', 'new_class2' );
foreach ( $add_classes as $class ) {
if ( ! in_array( $class, $classes ) ) {
$classes[] = sanitize_html_class( $class );
}
} // End of foreach loop
return $classes;
} // add_class()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment