Skip to content

Instantly share code, notes, and snippets.

@plaidpowered
Created May 27, 2022 15:56
Show Gist options
  • Save plaidpowered/20cbb0f487155302a1858482b1cef81c to your computer and use it in GitHub Desktop.
Save plaidpowered/20cbb0f487155302a1858482b1cef81c to your computer and use it in GitHub Desktop.
Add template classes to WordPress admin post editor
add_filter( 'admin_body_class', 'add_admin_body_template_class' );
function add_admin_body_template_class( $classes ) {
if ( ! is_admin() ) {
return $classes;
}
$queried_post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
if ( ! $queried_post_id ) {
return $classes;
}
$post = get_post( $queried_post_id );
if ( ! $post || is_wp_error( $post ) ) {
return $classes;
}
$template = get_page_template_slug( $post );
if ( $template ) {
$classes .= ' template-' . sanitize_title( $template );
}
if ( $post->ID === (int) get_option( 'page_on_front' ) ) {
$classes .= ' is-front-page';
}
if ( $post->ID === (int) get_option( 'page_for_posts' ) ) {
$classes .= ' is-blog-home';
}
if ( is_singular( $post ) ) {
$classes .= ' post-type-' . sanitize_title( $post->post_type );
}
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment