This is a quick guide that explains how to print the current user role as a body class in the WordPress Admin and/or the Frontend, it can also be used to print the user ID as a Class.
Why would you want to do this? Well if you have found this article then you probably already have a reason, we use it to style page elements for certain classes and even hide parts of a page to a user on both the frontend and backend. This isn’t the best method to hiding elements but can provide a temporary solution.
The function below will detect the current user and then print their role and id as a body class in the WP Admin and in the frontend of the site, for example, class="administrator user-id-1"
. This function will only run if the user is logged in.
/** * Add User Role Class to Body * Referenced code from http://www.studiok40.com/ */ function print_user_classes() { if ( is_user_logged_in() ) { add_filter('body_class','class_to_body'); add_filter('admin_body_class', 'class_to_body_admin'); } } add_action('init', 'print_user_classes'); /// Add user role class to front-end body tag function class_to_body($classes) { global $current_user; $user_role = array_shift($current_user->roles); $classes[] = $user_role.' '; return $classes; } /// Add user role class and user id to front-end body tag // add 'class-name' to the $classes array function class_to_body_admin($classes) { global $current_user; $user_role = array_shift($current_user->roles); /* Adds the user id to the admin body class array */ $user_ID = $current_user->ID; $classes = $user_role.' '.'user-id-'.$user_ID ; return $classes; return 'user-id-'.$user_ID; }
You can use this function in a MU plugin or as a standalone plugin or even in your theme functions.php.
Adam
Editor of the Lakewood Journal and the founder of Lakewood media. Also an avid landscape and travel photographer.
You may also like
How to fix Webmaster Console crawl errors in WordPress
How to easily fix Google Webmaster Console Errors on your WordPress site using a simple, free plugin. Improve your WordPress site SEO.
0 Comments3 Minutes
How to Improve Your Online Presence
3 steps to improve your digital presence. Whether your business is based online or not, all companies need to have some online presence in the digital age.
0 Comments4 Minutes
How to Hunt for Affordable WordPress Consultancy Services and Actually Hire One
Learn how to find WordPress consultancy services on Google, LinkedIn, and startup directories like Beta List and Product Hunt.
0 Comments14 Minutes
How to create Legal Pages for your WordPress website
Learn how to create Legal Pages in WordPress, use manual approach, WordPress plugins, or hire a Lawyer to create Legal Pages for your WordPress site.
0 Comments13 Minutes
How to Design a Site Experience Users Will Love
It's no secret that a website is basically the face of the company. It is the representation of its story and mission, as well as the company's main point of sale, in most cases.
0 Comments12 Minutes
How to optimise and compress your images
A quick guide to optimising, compressing and resizing your images on your Mac or Windows computer before uploading to your website.
0 Comments8 Minutes