# Add Useful Body Classes

> Adds the post type and slug of the current page plus a logged-in or logged-out class to the body tag, for precise CSS targeting.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: developer, css, body-class
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/extra-body-classes/

## Code

```php
add_filter( 'body_class', function ( $classes ) {
    if ( is_singular() ) {
        $post = get_queried_object();

        if ( $post instanceof WP_Post ) {
            $classes[] = sanitize_html_class( $post->post_type . '-' . $post->post_name );
        }
    }

    $classes[] = is_user_logged_in() ? 'is-logged-in' : 'is-logged-out';

    return $classes;
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Add Useful Body Classes" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
