Skip to content

Instantly share code, notes, and snippets.

@maheshwaghmare
Last active May 29, 2020 18:59
Show Gist options
  • Select an option

  • Save maheshwaghmare/f5953ba828a862108d2fd2e5f7989377 to your computer and use it in GitHub Desktop.

Select an option

Save maheshwaghmare/f5953ba828a862108d2fd2e5f7989377 to your computer and use it in GitHub Desktop.
Add / Remove classes from the body_class or post_class
<?php
function wpse15850_body_class( $wp_classes, $extra_classes )
{
// List of the only WP generated classes allowed
$whitelist = array( 'home', 'blog', 'archive', 'single', 'category', 'tag', 'error404', 'logged-in', 'admin-bar' );
// List of the only WP generated classes that are not allowed
$blacklist = array( 'home', 'blog', 'archive', 'single', 'category', 'tag', 'error404', 'logged-in', 'admin-bar' );
// Filter the body classes
// Whitelist result: (comment if you want to whitelist classes)
$wp_classes = array_intersect( $wp_classes, $whitelist );
// Blacklist result: (uncomment if you want to blacklist classes)
// $wp_classes = array_diff( $wp_classes, $blacklist );
// Add the extra classes back untouched
return array_merge( $wp_classes, (array) $extra_classes );
}
add_filter( 'body_class', 'wpse15850_body_class', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment