Skip to content

Instantly share code, notes, and snippets.

@mahfuzul
Created February 6, 2018 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahfuzul/39f7146a7b73e6ff56a0f1c382fa990e to your computer and use it in GitHub Desktop.
Save mahfuzul/39f7146a7b73e6ff56a0f1c382fa990e to your computer and use it in GitHub Desktop.
How to add or remove body classes in WordPress
<?php
// WP Add Remove Body Classes
// Add
add_filter( 'body_class','mh_body_classes' );
function my_body_classes( $classes ) {
$classes[] = 'class-name';
$classes[] = 'class-name-two';
if ( is_page_template( 'page-tempate_name.php' ) ) {
$classes[] = 'class-name-three';
}
return $classes;
}
// Remove
add_filter('body_class', function (array $classes) {
if (in_array('class-to-remove', $classes)) {
unset( $classes[array_search('class-to-remove', $classes)] );
}
return $classes;
});
// Remove
function mh_remove_body_class($classes, $class) {
foreach ( $classes as $key => $value ) {
if ( $value == 'error404' ) unset( $classes[ $key ] );
}
return $classes;
}
add_filter( 'body_class', 'st_remove_body_class', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment