ACF: Adding a Body Class from the Options Page

WordPress already does a pretty great job of adding several helper classes to the body element to assist within styling and functionality. But what if you’d like to add an additional one taken from an ACF field on your dashboard’s Options page?

In this example, I’ve set up a select field name on my theme’s Options page named ‘theme_colours’.

add_filter( 'body_class', 'options_body_class' );
function options_body_class( $classes ) {

	$theme_colour_choice = get_field('theme_colours', 'option');

	if ( $theme_colour_choice ) {
        $colour_choice = esc_attr( trim( $theme_colour_choice ) );

        $classes[] = $colour_choice;
	}

	return $classes;
}
Additional Reading:

Leave a Reply

Your email address will not be published.