Subscribe via RSS

We’re always on the lookout for new and better ways of improving old work. Today’s post will revisit “Unique Author Comment Styles in Wordpress“, a tutorial from last April that showed how to style the comments from a post’s author differently.

In this follow up, we’re going to do better. The basis for this upgrade falls on a messy PHP “if…else” statement that was present in the old method. Below, we’ll explore two different methods for updating your blog’s admin highlighting system.

The Manual Array Method

This time around, we’re going to make it easy for ourselves to add in additional administrators without much additional code. Instead of using a bloated OR check for each email address, we’ll just have Wordpress run through a preset array containing all of the emails of blog administrators. If the email of a commenter also belongs to an administrator, the comment will be assigned a separate style to make it stand out.

Build the Email Array

Before we’re able to have comments highlighted, we’ll need to specify which emails deserve special treatment. I’ve used some of Sam and I’s email addresses as an example.

  1. //List of emails to highlight
  2. $admin_emails = array(
  3. "",
  4. "",
  5. "",
  6. ""
  7. );
  8. ?>

You’ve Got Options

Emails do not have to belong to a registered user in this method. If you have multiple email accounts, the array can check for all of them. This opens up many more options for expansion.

The downside to this route is the loss of automation. Each email addresses must be entered in manually by the blog administrator. It would be much easier to have Wordpress automatically detect the administrator accounts for the blog installation. That’s where the second option comes in.

The Automated Database Method

If we wanted to, we can code this so that we’ll never have to manually insert emails into the comments.php file once it’s been set up. We can have Wordpress pull all of the email addresses of registered users with privileges of Editor and Administrator. For most blogs, this will fit your needs just fine.

Query the Database

We’ll first need to get all of the admin emails from the database and place them into an array. This is easy to do using the Wordpress database object, to query the database for specific users.

  1. //Automatically pull admin accounts
  2. $user_level = 8; //Default user level (1-10)
  3. $admin_emails = array(); //Hold Admin Emails
  4. //Search for the ID numbers of all accounts at specified user level and up
  5. $admin_accounts = $wpdb->get_results("SELECT * FROM $wpdb->usermeta WHERE meta_key = 'wp_user_level' AND meta_value >= $user_level ");
  6. //Get the email address for each administrator via ID number
  7. foreach ($admin_accounts as $admin_account){
  8. //Get database row for current user id
  9. $admin_info = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = $admin_account->user_id");
  10. //Add current user's email to array
  11. $admin_emails[$admin_account->user_id] = $admin_info->user_email;
  12. }
  13. ?>

If this is your first experience with the Wordpress database object (or databases in general) the code above may seem a little bit confusing. Here’s a breakdown of what’s going on to help sort it out:

  1. The $user_level variable contains a number 1-10. Wordpress user roles (e.g. Administrator, Editor, etc.) are all represented by a number value. To select Administrator accounts only, this number must be set to 8.
  2. The $admin_emails array will hold the emails returned from the query.
  3. The first database query searches the usermeta table for all users with a user level greater than or equal to the one stored in $user_level. The results are stored in the$admin_accounts array.
  4. Each item in the $admin_accounts array is processed, and the user table is queried using the user id number pulled from step 3. The email address is retrieved and stored in the $admin_emails array using the user id as an identifier in the array.

By the end of this code block, we have an array containing the email addresses of the specified user levels stored in $admin_emails. The nice part? This method will update itself when new users are registered. Just set it and forget it.

Marking Administrator Comments

At this point, both methods have the same next step. You’ll have an array of administrator emails stored in $admin_emails. We’ll need this as a reference when marking comments for special attention.

Filter the Comments

Now that we’ve got a list, we need to check each comment for admin email matches. If one is found, the comment will be assigned a class of “admincomment” and get different styling.

In the comments.php file of your blog theme, locate the foreach comment loop, and paste the following immediately under the start of it:

  1. //Default to
  2. $admin_comment = false;
  3. foreach ($admin_emails as $admin_email){
  4. //If comment was made from an admin email
  5. if($comment->comment_author_email == $admin_email){
  6. $admin_comment = true;
  7. break;
  8. }
  9. };
  10. ?>

The above code grabs the comment author’s email address and checks it against those in the admin email array. If a match is found, $admin_comment is set to true.

Assign Special Class to Comments

Special comments are marked with a class of “admincomment” by default. I’ve put a stripped down sample comment below to help you visualize. Pay special attention to the PHP included in the class attribute.

Remember that the comment structure must be after the $isByAdmin loop in the previous step in order to work.

Admin Comment in Action

Styling the Comments

Giving administrator comments some extra flair is the easy part. In both methods above, you’ll end up with a class of “admincomment” on comments to highlight. Since you’ve got a specific class to target, giving it some special styling is just an extra CSS style away.

I’ve included a sample CSS snippet below to help you get started, but feel free to get creative on your final product.

  1. .admincomment{background:#191919; color:#FFF; border:1px solid #333;}

Download Code Snippets

To help you along, I’ve put together a comment file with the necessary code snippets. It should help clear up any confusion left over about placement and ordering. You can grab the file using the link below:

Available Soon in Plugin Form

Not into the idea of building it yourself? Lucky for you, we’ve almost finished translating this into an easy to use class for Wordpress theme development.

The plugin version contains the methods shown above, as well as a few other useful ones not shown here. We’re planning to have it available for download in a few days. It will be the first project launch since Supersized.

Have any special feature requests? Leave them in the comments below and we’ll be sure to consider including them before making the final release.

Download : Click Here

Posted by ABDUL SABOOR Monday, October 5, 2009


Post a Comment