Skip to content

Instantly share code, notes, and snippets.

@evansolomon
Created July 27, 2012 06:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save evansolomon/3186427 to your computer and use it in GitHub Desktop.
Save evansolomon/3186427 to your computer and use it in GitHub Desktop.
Generic function to add body classes on the fly in WordPress
<?php
/**
* Generic function to add body classes
* Can take either a string of space-separated classes or an array
*
* Requires PHP 5.3 for access to closures
*/
function es_add_body_class( $new_classes ) {
// Turn the input into an array we can loop through
if ( ! is_array( $new_classes ) )
$new_classes = explode( ' ', $new_classes );
// Add a filter on the fly
add_filter( 'body_class', function( $classes ) use( $new_classes ) {
foreach( $new_classes as $new_class )
$classes[] = $new_class;
return $classes;
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment