Skip to main content
Snippets

Role Title, User ID, and Site ID in Admin Body Class

This is a simple and short method to pull the current user role title, user ID, and site id (MultiSite) then include it in the admin body class.

User Role Title

This will grab the first assigned user-role e.g. editor.

global $current_user;
	$user_roles = $current_user->roles;
	$user_role = array_shift($user_roles);
	return $user_role;

User Role ID

This will grab the user role ID.

global $user_level;
        return $user_level;

Site ID

This will grab the MultiSite blog / site ID.

get_current_blog_id()

Add to Body Class

To add these to the WordPress admin body class we use this.

	add_filter( 'admin_body_class', 'function' );

Let’s wrap all this together in a function so when it fires off we get everything into the admin body class.

function add_to_admin_body_class( $add_admin_body_classes ) {

	global $user_level;

	global $current_user;

	$user_roles = $current_user->roles;

	$user_role = array_shift($user_roles);

	{

	$add_admin_body_classes .= $user_role . ' user-role-'.$user_level .' site-id-'.get_current_blog_id() ;

	}
	return $add_admin_body_classes;
}

	add_filter( 'admin_body_class', 'add_to_admin_body_class' );

Your body tag will now reflect the standard WordPress classes as well as the ones we’ve added. It should look something like class="editor user-role-7 site-id-1"

Contact us to learn more about Adopt the Web for your business

Author Jarod Thornton

More posts by Jarod Thornton