WordPress

Highlighting admin comments in WordPress

Many WordPress users wish to visually highlight their own responses between the comments on a post, so that you recognize at the first glance which comments come from the site operator and which ones from normal visitors. So do I.
A Google search revealed that it is most often being recommended to simply compare the commentator’s user ID with your own one, and if they match, assign a second, special CSS class to the comment. That would look something like this:

<li class="comment<?php if($comment->user_id == 1) echo ' admincomment'; ?>">

I don’t like that very much. What if a site has multiple writers and you would like to highlight the comments of all of them? Should it look like this then?

<li class="comment<?php if($comment->user_id == 1 || $comment->user_id == 3 || $comment->user_id == 13 || $comment->user_id == 753) echo ' admincomment'; ?>">

I think we all agree that this is not very beautiful. ;) Besides, each time you admit a new member to your “editorial team”, you would have to fiddle around with the template again in order to enter his user ID there.
No, no, that’s not the way you do it. It would be a lot better if you simply checked the commentator’s user level, because if it is above 0, you know that you are dealing with one of the site’s “staff members”.

That will look like this:

<? $userdata = get_userdata($comment->user_id); ?>
<li class="comment<?php if($userdata->user_level > 0) echo ' admincomment'; ?>">

This way, all comments from contributors (level 1), authors (level 2–4), editors (level 5–7) and admins (level 8–10) will get a different style. Of course, you can even refine the whole thing with the aid of these levels, e. g. if you only want to change the appearance of admin and editor comments, but not the one of author and contributor comments.

Leave a comment Arrow

  1. 6th November, 2009

    Leo (Schnäppchenfuchs) GERMANY

    Gerade hier hätte ich einen Kommentar erwartet, um mir mal anschauen zu können, wie das bei dir aussieht ;)
    Bin mir noch nicht sicher was ich machen soll. ich bin der einzige Autor auf meinem Blog, d.h. die 1. Lösung wäre kein Problem. Meinst du die 2. Lösung “zieht” mehr Performance?

  2. 6th November, 2009

    Ginchen GERMANY

    Sooo *antwort* :D Wie Du siehst, kritzle ich hier einfach nur mit dem Rotstift, das ist alles. ;)

    Also, das Bißchen Performance dürfte sich wohl im Bereich von etwa einer Tausendstel Nanosekunde oder so befinden. ;) Von daher spricht eigentlich auch nichts gegen die zweite Lösung. Die andere gefällt mir einfach persönlich nicht, weil sie programmiertechnisch nicht alle Fälle für immer abdeckt. Wenn in Zukunft noch ein Autor dazu kommt, muß man wieder editieren, und dafür bin ich ja viel zu faul. :P Ich finde also die letzte Variante einfach nur eleganter.

  3. 20th February, 2010

    Julia GERMANY

    Hey, das ist echt ein netter Tip! Werd ich bei meinem nächsten Theme bedenken :-)

  4. 1st July, 2010

    michi AUSTRIA

    Hallo ich bin ein relativ neues Mitglied in der WordPress Gemeinde und möchte bei mir auch die Kommentare der Autoren hervorheben … aber dieses Tutorial hier verstehe ich nichtmal ansatzweise .. was muss ich bitte wo reinschreiben … ich blicks garnet :(

  5. 1st July, 2010

    Ginchen GERMANY

    Ja, Du hast recht, ich habe mich da wohl zu sehr an WordPress-Erfahrene gerichtet. ;)

    Also, zunächst mußt Du die Datei comments.php öffnen, die sich irgendwo in dem Ordner Deines Themes befinden sollte (also in wp-content/themes/name_des_themes). Dort muß es dann irgendwo eine Schleife geben, in der die Kommentare verarbeitet werden. Das sieht meist irgendwie so aus:

    <?php foreach ($comments as $comment) : ?>
    .
    .
    .
    <?php endforeach; ?>

    Und dazwischen sollte dann das Aussehen/der Aufbau der Kommentare definiert sein. Das kann (stark vereinfacht) beispielsweise so aussehen:

    <li class="comment"><?php comment_text(); ?></li>

    Und genau an dieser Stelle kann man dann beim class="comment" einhaken und je nach Kommentator zusätzlich weitere Klassen einfügen wie in meinem Post beschrieben.

    Ich hoffe, das war jetzt verständlich. :)

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <p> <pre lang="" line="" escaped=""> <q cite=""> <strike> <strong> | Code snippets can be posted in `backticks`. Example: `<?php echo "Hi!"; ?>`