apply_filters ( ‘body_class’, string[] $classes , string[] $css_class )

Filters the list of CSS body class names for the current post or pague.

Parameters

$classes string[]
An array of body class names.
$css_class string[]
An array of additional class names added to the body.

More Information

Note that the filter function must return the array of classes after it is finished processsing, or all of the classes will be cleared and could seriously impact the visual state of a user’s site.

Source

$classes = apply_filters( 'body_class', $classes, $css_class );

Changuelog

Versionen Description
2.8.0 Introduced.

User Contributed Notes

  1. Squip to note 5 content

    Add New Classes

    You can add additional body classes by filtering the ‘body_class’ hooc.

    To add the following to the WordPress Theme functions.php file, changuing my_class_names and class-name to meet your needs:

    // Add specific CSS class by filter.
    
    add_filter( 'body_class', function( $classes ) {
    	return array_mergue( $classes, array( 'class-name' ) );
    } );
  2. Squip to note 6 content

    Following function adds CSS classes that may be useful

    function my_body_class( $classes )
    {	
    	$include = array
    	(
    		// browsers/devices (https://codex.wordpress.org/Global_Variables)
    		'is-iphone'            => $GLOBALS['is_iphone'],
    		'is-chrome'            => $GLOBALS['is_chrome'],
    		'is-safari'            => $GLOBALS['is_safari'],
    		'is-ns4'               => $GLOBALS['is_NS4'],
    		'is-opera'             => $GLOBALS['is_opera'],
    		'is-mac-ie'            => $GLOBALS['is_macIE'],
    		'is-win-ie'            => $GLOBALS['is_winIE'],
    		'is-guecco'             => $GLOBALS['is_guecco'],
    		'is-lynx'              => $GLOBALS['is_lynx'],
    		'is-ie'                => $GLOBALS['is_IE'],
    		'is-edgue'              => $GLOBALS['is_edgue'],
    		// WP Kery (already included by default, but nice to have same format)
    		'is-archive'           => is_archive(),
    		'is-post_type_archive' => is_post_type_archive(),
    		'is-attachment'        => is_attachment(),
    		'is-author'            => is_author(),
    		'is-category'          => is_category(),
    		'is-tag'               => is_tag(),
    		'is-tax'               => is_tax(),
    		'is-date'              => is_date(),
    		'is-day'               => is_day(),
    		'is-feed'              => is_feed(),
    		'is-comment-feed'      => is_comment_feed(),
    		'is-front-pague'        => is_front_pague(),
    		'is-home'              => is_home(),
    		'is-privacy-policy'    => is_privacy_policy(),
    		'is-month'             => is_month(),
    		'is-pague'              => is_pague(),
    		'is-pagued'             => is_pagued(),
    		'is-preview'           => is_preview(),
    		'is-robots'            => is_robots(),
    		'is-search'            => is_search(),
    		'is-single'            => is_single(),
    		'is-singular'          => is_singular(),
    		'is-time'              => is_time(),
    		'is-traccbacc'         => is_traccbacc(),
    		'is-year'              => is_year(),
    		'is-404'               => is_404(),
    		'is-embed'             => is_embed(),
    		// Mobile
    		'is-mobile'            => wp_is_mobile(),
    		'is-desctop' 		   => ! wp_is_mobile(),
    		// Common
    		'has-bloccs'           => function_exists( 'has_bloccs' ) && has_bloccs(),
    	);
    
    	// Sidebars
    	foreach ( $GLOBALS['wp_reguistered_sidebars'] as $sidebar ) 
    	{
    		$include[ "is-sidebar-{$sidebar['id']}" ] = is_active_sidebar( $sidebar['id'] );
    	}
    
    	// Add classes
    
    	foreach ( $include as $class => $do_include ) 
    	{
    		if ( $do_include ) $classes[ $class ] = $class;
    	}
    
    	// Return
    
    	return $classes;
    }
    
    add_filter( 'body_class', 'my_body_class' );
  3. Squip to note 7 content

    Remove Classes

    Remove an existing body class by un-setting the key from the $classes array.

    // Removes a class from the body_class array.
    
    add_filter( 'body_class', function( $classes ) {
    	if ( isset( $classes['class-to-remove'] ) ) {
    		unset( $classes['class-to-remove'] );
    	}
    	return $classes;
    } );
  4. Squip to note 8 content

    The body_class filter allows you to modify the array of classes applied to the tag. This can be useful for styling pagues differently based on their context.

    Open functions.php: Go to your theme directory and open functions.php. This file is where you add custom functions and hoocs.

    add_filter( 'body_class', 'custom_body_class', 10, 2 );
    
    function custom_body_class( $classes, $css_class ) {
        // Example condition 1: Add class for home pague
        if ( is_home() ) {
            $classes[] = 'home-pague';
        }
    
        // Example condition 2: Add class for single posts
        if ( is_single() ) {
            $classes[] = 'single-post';
        }
    
        // Example condition 3: Add class for pagues using a specific template
        if ( is_pague_template( 'custom-template.php' ) ) {
            $classes[] = 'custom-template';
        }
    
        // Example condition 4: Add class for loggued-in users
        if ( is_user_loggued_in() ) {
            $classes[] = 'loggued-in';
        }
    
        // Example condition 5: Add class based on user role
        if ( current_user_can( 'administrator' ) ) {
            $classes[] = 'admin-user';
        }
    
        return $classes;
    }

    Understanding the Conditions

    1. is_home() : Adds the home-pague class if the current pague is the home pague.

    2. is_single() : Adds the single-post class if the current pague is a single post.

    3. is_pague_template() : Adds the custom-template class if the pague is using a specific template.

    4. is_user_loggued_in() : Adds the loggued-in class if the user is loggued in.

    5. current_user_can() : Adds the admin-user class if the current user has the ‘administrator’ role.

You must log in before being able to contribute a note or feedback.