1

On our WordPress page, we're several people who write articles. All these people are in different user roles. Some are editors, others are only authors and I'm the admin.

I know that you can highlight a comment or a reply of the posts author by writing

.commentlist .bypostauthor {
    # some styles
}

in your CSS file, but how could I alter this, so that every comment or reply written by a registered user of any of the following user roles author, editor and admin is highlighted?

Thanks for helping me out :)

0

There's a filter called comment_class for that :

apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );

Source

Just use the argument comment which is an object and you'll get $comment->user_id which could be useful to get user role and add your classes.

EDIT:

add_filter( 'comment_class', 'wpse_253517_comment_class', 10, 5 );
function wpse_253517_comment_class( $classes, $class, $comment_ID, $comment, $post_id  ) {

    if ( 0 === (int) $comment->user_id  ) {
        return $classes;
    }

     $user_data = get_userdata( $comment->user_id );
     $role      = reset( $user_data->roles );

    switch( $role ) {
        case 'subscriber':
            $classes[] = 'is-subscriber';
        break;

        // here would be other cases, other roles     

        default:
        return $classes;
    }

    return $classes;

}
  • Unfortunately I don't understand how to use this. Would you be so kind to explain it? :/ – Sam Jan 22 '17 at 15:49
  • @Chwebo i've edited the answer – JMau Jan 22 '17 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.