Last active
January 10, 2020 18:29
-
-
Save crewstyle/bde89befa6d5fb07e34a87f3bf25028e to your computer and use it in GitHub Desktop.
WordPress ~ Remove unnecessary details in body_class function - #frontend #security #optimisation
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 | |
/** | |
* Remove unnecessary details from `body_class()` | |
* | |
* @param string $classes | |
* @param string $class | |
* | |
* @uses get_query_var() | |
* @uses get_user_by() | |
* @uses get_userdata() | |
* @uses is_author() | |
* @uses sanitize_html_class() | |
* @uses get_queried_object() | |
* @uses get_queried_object_id() | |
*/ | |
add_filter('body_class', '_cw_clean_body_class', 10, 2); | |
function _cw_clean_body_class($classes, $class) | |
{ | |
// Remove CSS classes generated by WordPress on authors pages | |
if (isset($classes['author'])) { | |
// Get author | |
if (!$name = get_query_var('author_name')) { | |
$author = get_userdata(get_query_var('author')); | |
} else { | |
$author = get_user_by('slug', $name); | |
} | |
// Get data to remove | |
$removed[] = 'author-'.$author->ID; | |
$removed[] = 'author-'.$author->user_nicename; | |
// Remove classes | |
$classes = array_diff($classes, $removed); | |
} | |
// Remove CSS classes generated by WordPress on single pages | |
if (isset($classes['single'])) { | |
// Get post details | |
$post = get_queried_object(); | |
$pid = get_queried_object_id(); | |
// Check post type | |
if (isset($post->post_type)) { | |
$removed[] = 'single-'.sanitize_html_class($post->post_type, $pid); | |
$removed[] = 'postid-'.$pid; | |
// Remove classes | |
$classes = array_diff($classes, $removed); | |
} | |
} | |
// Remove CSS classes generated by WordPress on categories / tags / tax | |
if (isset($classes['category']) || isset($classes['tag']) || isset($classes['tax'])) { | |
// Get object details | |
$obj = get_queried_object(); | |
// Check object ID | |
if (isset($obj->term_id)) { | |
// Get class | |
$obj_class = sanitize_html_class($obj->slug, $obj->term_id); | |
$obj_class = is_numeric($obj_class) || !trim($obj_class, '-') ? | |
$obj->term_id : | |
$obj_class; | |
// Remove details | |
if (isset($classes['category'])) { | |
$removed[] = 'category-'.$obj_class; | |
$removed[] = 'category-'.$obj->term_id; | |
} else if (isset($classes['tag'])) { | |
$removed[] = 'tag-'.$obj_class; | |
$removed[] = 'tag-'.$obj->term_id; | |
} else if (isset($classes['tax'])) { | |
$removed[] = 'tax-'.sanitize_html_class($obj->taxonomy); | |
$removed[] = 'term-'.$obj_class; | |
$removed[] = 'term-'.$obj->term_id; | |
} | |
// Remove classes | |
$classes = array_diff($classes, $removed); | |
} | |
} | |
// Return all classes | |
return array_merge($classes, (array)$class); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment