WordPress Tutorial: Admin Body Classes

The WordPress admin interface is a clean easy to use GUI but there are times when you need to style admin content based on certain custom post types.

I recently had an issue where I needed to hide the date selector drop down select menu - the one with the option to 'Show all dates'.

The default 'Show all dates' menu is designed to filter posts by publish date and because my custom post type was a date based event management tool, I already had a custom date selector for events.

I first thought about hacking the core files as there is no filter for admin options such as the 'Show all dates' menu, but then I thought I would just hide the menu using some CSS.

.tablenav .actions select[name^="m"]{
display:none;
} 

The problem with this approach is that admin body classes are generic to every admin page view and post type, so any changes I made to CSS would be carried over to all admin views. To fix this issue simply include the PHP code in your functions.php file:

function jn_admin_body_class( $classes ) {
global $wpdb, $post;
$post_type = get_post_type( $post->ID );
if ( is_admin() && ( $post_type == 'basic_event' ) ) {
$classes .= 'post_type-' . $post_type;
}
return $classes;
}
add_filter( 'admin_body_class', 'jn_admin_body_class' ); 

I can now use the following CSS with no worries about the date selector being hidden on all post / page edit screens.

.post_type-basic_event .tablenav .actions select[name^="m"]{
display:none;
}