Add a Class to WordPress Body using body_class

For a WordPress website theme I was using the body_ class function in the body tag.

<body <?php body_class(); ?>>

This was adding relevant classes into the body.

I wished to add another class to the body. In this case for a customer  review. For a presentation I wished to provide a comparison between a modern responsive theme and an older style where the page doesn’t resize appropriately.

I added an unresponsive class to the WordPress website via the HTML body which would be used to stop a bootstrap based website from being responsive.

This is the relevant section of the header.php file

<?php wp_head(); ?>

</head>

<body <?php body_class(); ?>>
<a href="#content" class="skipnav">Skip to content</a>
<div id="page" class="hfeed site ">

To make the change I amended the functions.php file.

Within this you can add the additional classes via the body_class filter. I chose to add just the one class.

add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'unresponsive' ) );
} );

Reviewing the website source I was able to see the class added, within the body and the website was adopting the presentation fro the additional class.

References

https://developer.wordpress.org/reference/hooks/body_class/