How to add Class and Username to body class in WordPress.

Hello Developers,

Today I will show you how to add custom class and username with class in body. For that Refers below code.

Adding your custom class

add_filter( 'body_class', 'my_class_names' );
function my_class_names( $classes ) {
	global $current_user;
	$user_ID = $current_user->ID;
	$classes[] = 'Custom-body-Class' . $user_ID;
	return $classes;
}

Output:

Adding  your custom class with user name

add_filter( 'body_class', 'my_class_names1' );
function my_class_names1( $classes ) {
global $current_user;
		$user_ID = $current_user->display_name;
		$classes[] = 'Custom-body-Class-' . $user_ID;
		return $classes;
}

Output:

Thank you 🙂

Blog Catagory : WORDPRESS