Enable specific CSS Code for Visitors and specific Roles

how i can enable an specific css code for visitors and an specific user role

for example this code:

input[type=radio] {
    border: 1px solid !important;
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can add custom css on your html header using wp_head hook action & checking your current User Role

add_action('wp_head', 'add_css_of_current_specific_user_role');
function add_css_of_current_specific_user_role()
{ 
    /*Check Current User is Login*/
    if ( is_user_logged_in() ) 
    {  
       $user = wp_get_current_user();
       /*Check Current User Role*/
       if (in_array('visitor', $user->roles)) { ?>

            <style>
              // Some CSS
            </style>

    <?php } 

    }
}

Method 2

If you want to have specific CSS for visitors (aka not logged users) then you can take @HK89’s code and tweak it a bit

add_action('wp_head', 'add_css_of_current_specific_user_role');
function add_css_of_current_specific_user_role()
{ 
    /*Check if it's a visitor*/
    if ( !is_user_logged_in() ) 
    {  ?>
        <style>
            // Some CSS
        </style>
    <?php } 

    }
}

Method 3

i tried this code in the functions.php for showing to the guests this css

how is that possible

add_action('init', 'check_if_user_is_loggedin_function');
function check_if_user_is_loggedin_function()
{
    if ( is_user_logged_in() ) 
    {
} 
    else {
    echo '<style>
            .woo-variation-swatches.wvs-style-squared .variable-items-wrapper .variable-item.button-variable-item-offen {
            display: none;
            }
            .woo-variation-swatches.wvs-style-squared .variable-items-wrapper .variable-item.color-variable-item-offen {
            display: none;
            }
        </style>';
}
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x