Support » Plugins » Hacks » Mark admin and editor comments

  • Hi there

    Trying to figure out how to mark all admin and editor comments in the comment list. Ideally I would like to check if the author of the comment has an email ending with @domain.com – if true, do something.

    Any ideas?

Viewing 4 replies - 1 through 4 (of 4 total)
  • WordPress adds the bypostauthor class to all comments that are by the author of the post and byuser and comment-author-username to comments left by logged-in users. Will being able to single out comments that are by a particular user or the author of the post do what you’re looking for?

    Edit:
    Also, you can use the comment_class filter to conditionally add classes. Adding this to your theme’s functions.php might do the trick:

    function change_comment_classes( $classes ) {
    
        if ( current_user_can( 'administrator' ) ) {
            $classes[] = 'byadmin';
        } elseif ( current_user_can( 'editor' ) ) {
            $classes[] = 'byeditor';
        }
    
        return $classes;
    }
    
    add_filter( 'comment_class' , 'change_comment_classes' );

    I haven’t tested that exact code so I’m not entirely sure it works as-is.

    Thread Starter Kenneth Jensen

    (@kennethj)

    Hey there. Looks good!

    Im not that sharp on PHP, but im pretty sure that this could work. I’ll give a try later tonight and get back with the (hopefully) good news.

    Big Bagel had the right idea, however current_user_can will check against the current user, not the user a comment belongs to.. so you’ll likely see incorrect filtering as a result..

    If you take a look at where the filter is defined though, it should give you some ideas on how to identify the owner of the comment inside your filter.
    http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/comment-template.php#L299

    Indeed. I should have checked that out before suggesting it. 🙂

    This is how it should be done (if I didn’t screw up something again):

    function change_comment_classes( $classes, $class, $comment_id, $post_id ) {
    
        $comment = get_comment( $comment_id );
    
        if ( user_can( $comment->user_id, 'administrator' ) ) {
            $classes[] = 'byadmin';
        } elseif ( user_can( $comment->user_id, 'editor' ) ) {
            $classes[] = 'byeditor';
        }
    
        return $classes;
    }
    
    add_filter( 'comment_class' , 'change_comment_classes', 10, 4 );

    Edit:
    This isn’t really important, but since you don’t want to add any classes if the author isn’t a registered user, you could also add something like this right after $comment = get_comment( $comment_id ); to keep from unnecessarily calling user_can():

    if ( $comment->user_id == 0 ) {
        return $classes;
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Mark admin and editor comments’ is closed to new replies.