Site ID and Path Classes on the Body Tag
Adds classes such as site-3 and site-shop to the body tag on every network site, so one shared stylesheet can target individual sites.
Opens WPIDE on
Code
if ( ! is_multisite() ) {
return;
}
add_filter( 'body_class', function ( $classes ) {
$site = get_site();
$classes[] = 'site-' . get_current_blog_id();
$classes[] = is_main_site() ? 'site-main' : 'site-sub';
$slug = trim( (string) ( $site ? $site->path : '' ), '/' );
if ( '' === $slug && $site ) {
$slug = strtok( $site->domain, '.' );
}
if ( '' !== $slug ) {
$classes[] = 'site-' . sanitize_html_class( $slug );
}
return $classes;
} );
Notes
""
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.