Ask your WordPress questions! Pay money and get answers fast! (more info)

Remove Unused Class WordPress

  • SOLVED

I would a function to remove the following unused "class" generated by Wordpress. So far I'm seeing 3 parts that are being automatically added the unused class which is body, menu, and comment. The body and menu class can be totally removed, while for the comment, the "bypostauthor" class is required when the comment is posted by the admins.


BODY
====
<body id="blog" class="single single-post postid-29679 single-format-standard main"
<body id="blog" class="home blog main"


MENU
====
<li id="menu-item-22393" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home"


COMMENT (Need to maintain the "bypostauthor" class)
=======
<li class="comment even thread-even depth-1"
<li class="comment byuser comment-author-roy bypostauthor odd alt depth-2"

Thank you.

Answers (4)

2015-03-17

Shoeb mirza answers:

Did you try $whitelist & $blacklist?


Shoeb mirza comments:

For body use this

add_filter('body_class','my_class_body');
function my_class_body($classes) {
return array();
}


Shoeb mirza comments:

For comments, post this code




add_filter( 'comment_class', 'comment_comment_class', 10, 2 );

function comment_comment_class( $wp_classes, $extra_classes ) {


$whitelist = array( 'bypostauthor', 'home', 'error404' );


$wp_classes = array_intersect( $wp_classes, $whitelist );


return array_merge( $wp_classes, (array) $extra_classes );
}


Shoeb mirza comments:

and for menu

add_filter('wp_page_menu','my_class_body');
function my_class_body($classes) {
return array();
}



2015-03-17

Reigel Gallarde answers:

it is better to use some kind of whitelist... so that deleted class are just the unwanted ones...

for the body, you can follow this...

[[LINK href="http://wordpress.stackexchange.com/a/15878/974"]]http://wordpress.stackexchange.com/a/15878/974[[/LINK]]
add_filter( 'body_class', 'wpse15850_body_class', 10, 2 );

function wpse15850_body_class( $wp_classes, $extra_classes ) {

// List of the only WP generated classes allowed
$whitelist = array( 'portfolio', 'home', 'error404' );

// Filter the body classes
$wp_classes = array_intersect( $wp_classes, $whitelist );

// Add the extra classes back untouched
return array_merge( $wp_classes, (array) $extra_classes );
}


you could do the same logic with the filters for the menu and comment...

for the menu

add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);


for the comment

add_filter( 'comment_class' , 'comment_classes' );



Reigel Gallarde comments:

you could also PM me and I'll be happy to do it for you.. [[LINK href="http://www.wpquestions.com/user/contact/id/78975"]]PM me.[[/LINK]]

2015-03-17

timDesain Nanang answers:

Hi Sir, Try this code:

//remove all body class
function wpq_body_class( $classes ) {
unset($classes);
$update = array();

return $update;
}
add_action('body_class', 'wpq_body_class');

//remove all nav_menu class
function wpq_nav_class( $classes, $item ) {
unset($classes);
$update = array();

return $update;
}
add_filter( 'nav_menu_css_class', 'wpq_nav_class', 10, 2 );

//remove unnecessary comment class
function wpq_comment_classes( $classes ) {
$update = array();

foreach($classes as $class) :
if( strpos($class, 'bypostauthor')!==false ){
$update[] = $class;
}
endforeach;

return $update;
}
add_filter( 'comment_class' , 'wpq_comment_classes' );


raymond comments:

Hi tim, thanks for the complete code.
I noticed that the "//remove unnecessary comment class" code still adds an empty class such as:

<li class="" >

Is it possible to just display it as <li> without the empty class="" ?

2015-03-17

Dbranes answers:

Hi Raymond

If you have a child theme or custom theme, you should just remove the <em>body_class()</em> function from:

<body <?php body_class(); ?>>


Regarding the filtering, you could simplify it to:

add_filter( 'body_class', '__return_empty_array', PHP_INT_MAX );
add_filter( 'nav_menu_css_class', '__return_empty_array', PHP_INT_MAX );

add_filter( 'comment_class', function( $classes ) {
return array_intersect( $classes, array( 'bypostauthor' ) );
}, PHP_INT_MAX );


and by using <em>PHP_INT_MAX</em> there's no change for plugins to add extra classes.

ps: I'm afraid you have to perform a replacement with output buffering to get rid of empty comment classes.


raymond comments:

Thanks, I've managed to do what I wanted with your code.

I believe Dbranes has given a shorter and more optimized code and its fair for me to award the prize to him?


Dbranes comments:

great, regarding the prize I'd be happy to share it with the others