{"id":1363,"date":"2009-11-16T22:24:50","date_gmt":"2009-11-16T20:24:50","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1363"},"modified":"2009-11-16T22:24:50","modified_gmt":"2009-11-16T20:24:50","slug":"how-to-style-editor-or-any-role-comments-in-wordpress","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2009\/11\/how-to-style-editor-or-any-role-comments-in-wordpress\/","title":{"rendered":"How To Style Editor (Or Any Role) Comments in WordPress"},"content":{"rendered":"<p>I&#39;ve recently moved to WordPress an active community site which is run and moderated by several editors and administrators. Each post gets dozens of comments, and I wanted to highlight those made by staff members.<\/p>\n<h2>What not to do<\/h2>\n<p>If you&#39;ve googled a bit for a solution on how to identify authors&#39; or admins&#39; comments, you&#39;ve probably seen several explanations on how to implement this by comparing emails. Something like:<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;?php<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">if<span style=\"color:#006600;font-weight:bold\">&#040;<\/span> $comment-&gt;<span>comment_author_email<\/span> == <span style=\"color:#CC0000\">'admin@blog.com'<\/span> <span style=\"color:#006600;font-weight:bold\">&#041;<\/span> <span style=\"color:#006600;font-weight:bold\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; $class = <span style=\"color:#CC0000\">'admin'<\/span>;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color:#006600;font-weight:bold\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">?&gt;<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>While this is sort of OK for a single author weblog, it&#39;s totally inconvenient and not scalable for a community site run by several editors and administrators: you would need to edit the template file whenever there&#39;s a new member in the staff, and nothing prevents an unlogged user from commenting using your well known email address.<\/p>\n<p>So, what to do instead?<br \/>\n<!--more--><\/p>\n<h2>First, collect all users and their role<\/h2>\n<p>WordPress function <tt>get_users_of_blog()<\/tt> returns an array of all users with their user_id and role. We&#39;re going to loop through all users and collect their id&#39;s and roles in an array.<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">$users = get_users_of_blog();<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">foreach( $users as $user ) {<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; $role = array_pop( array_keys( unserialize($user-&gt;meta_value) ) ); \/\/ yeah, I know, looks awkward :)<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; $user_id = $user-&gt;user_id;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; if ( $role != 'subscriber' ) \/\/ collect id of anyone whose role is greater than 'subscriber'<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; $roles[ $user_id ] = $role;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">}<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>At this point, the array <tt>$roles<\/tt> is populated with something like:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">$roles = array (<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; '1' =&gt; 'administrator',<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; '2' =&gt; 'editor',<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; '3' =&gt; 'editor',<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; '4' =&gt; 'administrator',<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; '5' =&gt; 'author',<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">);<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h2>Now identify for each comment the commenter&#39;s role<\/h2>\n<p>Typically, your theme has a file named <tt>comments.php<\/tt> with a loop that looks like this:<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;ol id=&quot;commentlist&quot;&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;?php foreach <span style=\"color:#006600;font-weight:bold\">&#040;<\/span>$comments as $comment<span style=\"color:#006600;font-weight:bold\">&#041;<\/span> : ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;li&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;p&gt;By &lt;?php comment_author_link<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt; - &lt;?php comment_date<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt;&lt;\/p&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;?php comment_text<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;\/li&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;?php endforeach; ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;\/ol&gt;<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>WordPress fetches every comment as an object containing, among others, the following informations:<\/p>\n<div id=\"ig-sh-5\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">stdClass Object (<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; [comment_author] =&gt; Joe<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; [comment_author_email] =&gt; joe@email.com<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; [comment_author_url] =&gt; http:\/\/joe.com\/<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; [comment_author_IP] =&gt; 100.101.102.103<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; [user_id] =&gt; 4<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">)<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>If the user is logged in, <tt>$comment->user_id<\/tt> is an integer greater than 0. If the user is not logged in (assuming non logged users can comment), <tt>$comment->user_id<\/tt> is zero.<\/p>\n<p>A trivial way to get an user&#39;s role would be to issue a call to <tt>get_userdata( $user_id )<\/tt> within the comment loop but that would add an extra query per logged in user, which is an unnecessary burden if you an active community of commenters and a lot of registered users.<\/p>\n<p>Instead we&#39;ll simply compare each user_id with what we&#39;ve collected at the beginning. The comment loop would be something like:<\/p>\n<div id=\"ig-sh-6\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;ol id=&quot;commentlist&quot;&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;?php foreach <span style=\"color:#006600;font-weight:bold\">&#040;<\/span>$comments as $comment<span style=\"color:#006600;font-weight:bold\">&#041;<\/span> : ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;?php <span style=\"color: #666666;font-style: italic\">\/\/ The extra stuff to get commenter's role<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; $user_id = $comment-&gt;<span>user_id<\/span>;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; $role = <span style=\"color:#006600;font-weight:bold\">&#040;<\/span> isset<span style=\"color:#006600;font-weight:bold\">&#040;<\/span> $roles<span style=\"color:#006600;font-weight:bold\">&#091;<\/span>$user_id<span style=\"color:#006600;font-weight:bold\">&#093;<\/span> <span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ? $roles<span style=\"color:#006600;font-weight:bold\">&#091;<\/span>$user_id<span style=\"color:#006600;font-weight:bold\">&#093;<\/span> : <span style=\"color:#CC0000\">''<\/span> <span style=\"color:#006600;font-weight:bold\">&#041;<\/span>;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;li class=&quot;&lt;?php echo $role; ?&gt;&quot;&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;p&gt;By &lt;?php comment_author_link<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt; - &lt;?php comment_date<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt;&lt;\/p&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;?php comment_text<span style=\"color:#006600;font-weight:bold\">&#040;<\/span><span style=\"color:#006600;font-weight:bold\">&#041;<\/span> ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &lt;\/li&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;?php endforeach; ?&gt;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&lt;\/ol&gt;<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Now, identifying staff comments is just a matter of CSS:<\/p>\n<div id=\"ig-sh-7\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">#commentlist li { border:2px solid white; } \/* not logged or subscriber *\/<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">#commentlist li.administrator { border:2px solid red } \/* blog admin *\/<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">#commentlist li.editor { border:2px solid blue } \/* editor *\/<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h2>All in all<\/h2>\n<p>All in all, this very simple method only adds one extra query to the whole page (issued by <tt>get_users_of_blog()<\/tt>). You could even easily get rid of that extra query with caching in a flat .php file the array produced by <tt>get_users_of_blog()<\/tt> and refresh it with hooking into &#39;set_user_role&#39; (see <tt>wp-includes\/capabilities.php<\/tt>)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve recently moved to WordPress an active community site which is run and moderated by several editors and administrators. Each post gets dozens of comments, and I wanted to highlight those made by staff members. What not to do If you&#39;ve googled a bit for a solution on how to identify authors&#39; or admins&#39; comments, you&#39;ve probably seen several explanations\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,324,245],"class_list":["post-1363","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-comments","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1363"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1363\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}