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' );
Default image
Austin
πŸ’πŸ½β€β™‚οΈ Husband to Jeana. ⚾️ Dodgers obsessed. πŸ† World Series Champs. πŸ’» Lead Software Engineer @ Beachbody. πŸŒπŸΌβ€β™‚οΈScratch golfer. 🌱 Hydroponic Gardner. 🍿 Plex nerd. πŸš™ '15 WRX & '66 Corvette C2. 🐢 Missing my love Keiki; RIP 😒. Follow me on Twitter @TheFrosty & Instagram @TheFrosty.
Articles: 285

2 Comments

Leave a Reply

%d bloggers like this: