Extending the WordPress admin_body_class

A small code snippet to extend the admin body class inside the WordPress. Great for WordPress plugin and theme developers!

I’m working on a new WordPress theme for release with 3.0 that includes many new post_types. One problem I found was that if I wanted to not include the editor but allow for photo uploads, the Insert into Post button would no longer appear. But I didn’t want the end user to have access to the editor. So I thought I would remove or hide the editor with CSS or DOM manipulation. One problem I noticed is the admin classes are pretty weak, so I thought to beef it up with a custom class on my post_types.

Here is the snippet to extend the WordPress admin_body_class.

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

This will then output on my post_type Photo: post_type-photo. Sweet!

Don’t forget to add the correct filter:

add_filter( 'admin_body_class', 'wpwag_admin_body_class' );
Austin
Austin

πŸ’πŸ½β€β™‚οΈ Husband to Jeana.
⚾️ Dodgers & Brewers.
πŸ’» PHP Engineer.
πŸŒπŸΌβ€β™‚οΈGolfer; ~14 HDC.
🌱 Hydroponic Gardner.
🍿 Plex nerd.
πŸš™ '15 WRX, '22 Model 3 LR, & '66 Corvette C2.

Follow me on Twitter @TheFrosty & Instagram @TheFrosty.

Articles: 292

2 Comments

Comments are closed.