M
M
mr_firuzinho2016-02-11 06:38:52
CMS
mr_firuzinho, 2016-02-11 06:38:52

How to add class to body tag using php?

How to make it so that when entering the site {made on wordpress} with the choice of a specific role {subscriber, author}, a class with the value of the role is added to the body tag. For example, if you go to the site by selecting the subscriber role, then the subscriber class was added to the body tag?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WP Panda, 2016-02-15
@mr_firuzinho

function role_body_class($classes) {
    
    if (is_user_logged_in()) {
        $user_data = get_userdata(get_current_user_id());
        $classes[] = $user_data->roles[0];
    }

    return $classes;

}

add_filter( 'body_class','role_body_class' );

S
Silm, 2016-02-11
@Silm

<body class="<?=isset($class) ? $class : ''?>">
...
</body>

The $class variable must have previously been assigned the class name. If the variable is not defined the class is not added.
Read the manual first.

E
eskrano, 2016-02-11
@eskrano

write the condition in the code and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question