{"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 class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">&lt;?php\r\nif( $comment-&gt;comment_author_email == &#039;admin@blog.com&#039; ) {\r\n\t$class = &#039;admin&#039;;\r\n}\r\n?&gt;<\/code><\/pre><\/div>\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 class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">$users = get_users_of_blog();\r\nforeach( $users as $user ) {\r\n\t$role = array_pop( array_keys( unserialize($user-&gt;meta_value) ) ); \/\/ yeah, I know, looks awkward :)\r\n\t$user_id = $user-&gt;user_id;\r\n\tif ( $role != &#039;subscriber&#039; ) \/\/ collect id of anyone whose role is greater than &#039;subscriber&#039;\r\n\t\t$roles[ $user_id ] = $role;\r\n}<\/code><\/pre><\/div>\n<p>At this point, the array <tt>$roles<\/tt> is populated with something like:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-3\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">$roles = array (\r\n\t&#039;1&#039; =&gt; &#039;administrator&#039;,\r\n\t&#039;2&#039; =&gt; &#039;editor&#039;,\r\n\t&#039;3&#039; =&gt; &#039;editor&#039;,\r\n\t&#039;4&#039; =&gt; &#039;administrator&#039;,\r\n\t&#039;5&#039; =&gt; &#039;author&#039;,\r\n);<\/code><\/pre><\/div>\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 class=\"igsh-code-box\" id=\"ig-sh-4\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">&lt;ol id=&quot;commentlist&quot;&gt;\r\n&lt;?php foreach ($comments as $comment) : ?&gt;\r\n\t&lt;li&gt;\r\n\t&lt;p&gt;By &lt;?php comment_author_link() ?&gt; - &lt;?php comment_date() ?&gt;&lt;\/p&gt;\r\n\t&lt;?php comment_text() ?&gt;\r\n\t&lt;\/li&gt;\r\n&lt;?php endforeach; ?&gt;\r\n&lt;\/ol&gt;<\/code><\/pre><\/div>\n<p>WordPress fetches every comment as an object containing, among others, the following informations:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-5\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">stdClass Object (\r\n    [comment_author] =&gt; Joe\r\n    [comment_author_email] =&gt; joe@email.com\r\n    [comment_author_url] =&gt; http:\/\/joe.com\/\r\n    [comment_author_IP] =&gt; 100.101.102.103\r\n    [user_id] =&gt; 4\r\n)<\/code><\/pre><\/div>\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 class=\"igsh-code-box\" id=\"ig-sh-6\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">&lt;ol id=&quot;commentlist&quot;&gt;\r\n&lt;?php foreach ($comments as $comment) : ?&gt;\r\n\t&lt;?php \/\/ The extra stuff to get commenter&#039;s role\r\n\t$user_id = $comment-&gt;user_id;\r\n\t$role = ( isset( $roles[$user_id] ) ? $roles[$user_id] : &#039;&#039; );\r\n\t?&gt;\r\n\t&lt;li class=&quot;&lt;?php echo $role; ?&gt;&quot;&gt;\r\n\t&lt;p&gt;By &lt;?php comment_author_link() ?&gt; - &lt;?php comment_date() ?&gt;&lt;\/p&gt;\r\n\t&lt;?php comment_text() ?&gt;\r\n\t&lt;\/li&gt;\r\n&lt;?php endforeach; ?&gt;\r\n&lt;\/ol&gt;<\/code><\/pre><\/div>\n<p>Now, identifying staff comments is just a matter of CSS:<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-7\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">#commentlist li { border:2px solid white; } \/* not logged or subscriber *\/\r\n#commentlist li.administrator { border:2px solid red } \/* blog admin *\/\r\n#commentlist li.editor { border:2px solid blue } \/* editor *\/<\/code><\/pre><\/div>\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}]}}