[resolved] How to highlight own comment per user if logged in? (5 posts)

  1. 1BJK903
    Posted 3 years ago #

    Hi all,

    If a registered user leaves a reply on my post, I want to give their own comments a different color so that they can see that they have posted on that particular post already... I see unique classes like comment-author-admin, but I don't want to style my 500 users apart, because that makes the site slow... How can I style/highlight THEIR own comments?

  2. David Choi
    Posted 3 years ago #

    The idea will be:

    1. Create a child theme, copy the comment.php file to your child them folder and create a new style.css file


    2. Edit the comment.php file, so you can include a CSS class name if the visitor viewing the page (logged in user) is the same as the comment user using the function wp_get_current_user()


    3. Add that CSS you've added to the child theme's style.css file to style it.

    This will only work with logged in users. Although you can make it work for visitors (not logged in users) using cookies, that's not really reliable.

  3. Michael
    Forum Moderator
    Posted 3 years ago #

    alternatively, without editing the comments code directly, you can use some code in functions.php of the child theme to add a corresponding class to the comments:

    add_filter( 'comment_class', 'comment_class_logged_in_user' );
    function comment_class_logged_in_user( $classes ) {
    	global $comment;
    	if ( $comment->user_id > 0 && is_user_logged_in() ) {
    		global $current_user; get_currentuserinfo();
    		$logged_in_user = $current_user->ID;
    		if( $comment->user_id == $logged_in_user ) $classes[] = 'comment-author-logged-in';
    return $classes;

    and in style.css of the child theme, add some formatting with:

    .comment-author-logged-in { background: orange; }

  4. 1BJK903
    Posted 3 years ago #


    You are a true legend. Thanks mate!


    Thanks for the effort!

  5. 1BJK903
    Posted 3 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic