Last active
January 7, 2022 21:40
-
-
Save mi-ca/ca4abe75b640e26112ca281d19e64e61 to your computer and use it in GitHub Desktop.
remove useless classes from body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add and remove body_class() classes | |
*/ | |
function mica_body_class($classes) { | |
// Add post/page slug if not present | |
if (is_single() || is_page() && !is_front_page()) { | |
if (!in_array(basename(get_permalink()), $classes)) { | |
$classes[] = basename(get_permalink()); | |
} | |
} | |
// Remove unnecessary classes | |
$home_id_class = 'page-id-' . get_option('page_on_front'); | |
$uslesspage_id_class= 'page-id-'.get_the_ID(); | |
$usless_id_class= 'postid-'.get_the_ID(); | |
$remove_classes = [ | |
'page-template-default', | |
'blog', | |
'single-post', | |
'postid-1', | |
'single-format-standard', | |
$home_id_class, | |
$usless_id_class, | |
$uslesspage_id_class | |
]; | |
$classes = array_diff($classes, $remove_classes); | |
return $classes; | |
} | |
add_filter('body_class', __NAMESPACE__ . '\\mica_body_class'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment