Remove Usernames From CSS Classes
Strips the author and commenter usernames WordPress adds to body and comment classes, closing a quiet way to discover login names.
Opens WPIDE on
Code
add_filter( 'comment_class', function ( $classes ) {
return array_values( array_filter( $classes, function ( $class ) {
return 0 !== strpos( $class, 'comment-author-' );
} ) );
} );
add_filter( 'body_class', function ( $classes ) {
if ( ! is_author() ) {
return $classes;
}
$author = get_queried_object();
if ( $author instanceof WP_User ) {
$classes = array_diff( $classes, array( 'author-' . sanitize_html_class( $author->user_nicename, $author->ID ) ) );
}
return array_values( $classes );
} );
Notes
Check that your theme CSS does not rely on these classes.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.