• Resolved ofmarconi

    (@ofmarconi)


    Hi!

    Could you tell me how to add to BodyClass the class referring to the current user’s roll?

    If this is not possible, I would like to request this feature.

    So we can change the style according to user role, and even hide with display: None some things.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi,

    // Add current user role to the front end page body element
    add_action('wp_head', 'add_current_user_role_class');
    
    function add_current_user_role_class() {
    
        if ( is_admin() ) {
            // do not show at back-end
            return;
        }
        
        if ( !is_user_logged_in() ) {
            return;
        }
        
        $user = wp_get_current_user();
        $user_roles = array_values( $user->roles );
        $role = array_shift( $user_roles );
    ?>    
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                var body = document.body;
                body.classList.add('<?php echo $role; ?>');
            });
        </script>
    <?php
    }
    Thread Starter ofmarconi

    (@ofmarconi)

    Wow

    Amazing!

    To display on the backend is possible?

    I deleted this part of the code and it seems that still does not work on the backend

        if ( is_admin() ) {
            // do not show at back-end
            return;
        }

    Thx 4ever!

    • This reply was modified 2 years, 6 months ago by ofmarconi.
    • This reply was modified 2 years, 6 months ago by ofmarconi.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Body Class’ is closed to new replies.