WordPress Tip: Styling Comments by Tagging Commenters

Tagged:
7

I received a few emails asking me about the tagging description that I put next to the commenters name in the comment section. I did not use a plugin for that and I just hack a quick code in my functions.php. It is a pretty way to show who’s who on your blog. :) Download the functions below. It will be easier for you to read it. Explanations are also provided in this file.

Download Here

The CSS
So let me share the CSS code first. You can tweak it the way you want it. I’ll just share the current version of CSS that I have for this code snippet.


/* Main Style for the Tags */
span.user-tag {
	font-size:9.5px;
	text-transform:uppercase;
	-webkit-border-radius:2px;
	-moz-border-radius:2px;
	border-radius:2px;
	padding:1px 2px;
}
/* Post Author Tag (That'll be you) */
span.user-tag.bypostauthor {
	background:#fe57a1;
	bottom:2px;
	color:#FFF;
	font-size:9.5px;
	margin-left:5px;
	position:relative;
	padding:3px 5px;
}
/* Best Buds Tag (That'll be your friends) */
span.user-tag.bestbuds {
	background:#204117;
	bottom:2px;
	color:#FFF;
	font-size:9.5px;
	margin-left:5px;
	position:relative;
	padding:3px 5px;
}

The functions.php
This is where the fun begins! You should put this code in your functions.php. Put each of your commenters’ email in an array. We will use their emails to determine who’s who on your blog, just like Gravatar. We will use a WordPress built-in function to get the commenter’s email: get_comment_author_email($comment).

Group them by their categories. In my case, I group them as “blogging buddy”, “best buds”, etc. Then, you can just display the HTML span line according to each group. You can achieve this by using conditional statement (if/else).


function bones_user_tag($comment) {
   // Blogging buddy array
   $blogging_buddy = array('someone@something.net', 'b@aa.net');
   // Best buds array
   $best_buds = array('someone@something.net', 'some@some.net');
   // Blogging buddy and best buds array
   $blogging_buddy_and_best_buds = array('b@aa.net');
   // Conditional Statement starts here
   // Comment Author/Blog Owner
   if (1 == $comment->user_id) {
    echo '<span class="user-tag bypostauthor">Author</span>';
   // Blogging Buddy
    } elseif (in_array(get_comment_author_email($comment), $blogging_buddy)) {
       echo '<span class="user-tag popularcommenter">Blogging Buddy</span>';
   // My Mom. Did not use any arrays here. I only have one mom :P  I just use the old '==' sign
   } elseif (get_comment_author_email($comment) == "something@gmail.com") {
       echo '<span class="user-tag mom">Hello Mommy!</span>';
   // My Best Buds
   } elseif (in_array(get_comment_author_email($comment), $best_buds)) {
       echo '<span class="user-tag bestbuds">Best Buddy</span>';
   // My best buds who happened to be my blogging buddy as well
   } elseif (in_array(get_comment_author_email($comment), $blogging_buddy_and_best_buds)) {
       echo '<span class="user-tag popularcommenter">Blogging Buddy</span>';
       echo '<span class="user-tag bestbuds">Best Buddy</span>';
   }
}

We will first check if the comment belongs to the blog owner. If it doesn’t, it will go through the elseif line. We use the in_array method to check whether the array contains the commenter’s email. in_array(get_comment_author_email($comment), $best_buds) code will check whether $best_buds array contains the commenter’s email. If it returns true, it will show the colored tags. Otherwise, it will return nothing.

Calling the Function
After you had created the function, you need to call it somewhere. You call it next to where you would output your commenter’s name. This is how my comments function look like. The newly added function is highlighted in grey.


<article id="comment-<?php comment_ID(); ?>" class="clearfix">
  <header class="comment-author vcard">
      <?php echo get_avatar($comment,$size='50',$default='<path_to_url>' ); ?>
      <?php printf(__('<cite class="fn">%s</cite>'), get_comment_author_link()) ?>
      <time datetime = "<?php echo get_comment_date('c'); ?>"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s'), get_comment_date(),  get_comment_time()) ?></a></time>
      <?php edit_comment_link(__('(Edit)'),'  ','') ?>
      // user tag function is added here:
      <?php bones_user_tag($comment); ?>
      </header>

My comment code may look different than yours but you get the idea: just call the function next to your commenters’ name. Call it like this: <?php bones_user_tag($comment); ?>

What if your one of your commenters have multiple role? For example, Flippy Doodle here is both my blogging buddy and one of my best buds (we know each other in real life).

In this case, I just create an array named $blogging_buddy_and_best_buds and just use the in_array function again to check whether the commenter’s email match any of the array values. I included this in the code snippet above. You can refer to it or download the file that is provided for you.

Anyway, try this out and see if it works. Post your questions here if you have one. In the mean time, I’ll try and make the code above more elegant. It was a quick hack anyway. :P Happy tagging! (I think it is about time that I change the style of my pre code tag. It makes the code look messy and hard to read. Sorry if you have trouble reading them. Just download the file above if you must. :( )

Winter Break: Time to Laze Around

Tagged:
3

I am finally done with the semester and I can’t wait to fly back to Malaysia tomorrow. Most of my grades are out and I hope I get an A in the last one as well. I did well this semester despite being a full time procrastinator. ;)

School Life
Well, I am not really looking forward to next semester (as always). I have this core course that I am a bit nervous about; it is a project based course and it involves group work. Group members are assign to you and you don’t get to choose them. Well, lets hope I get into a good group. It is not that I am difficult to work with. In fact, I am a team player but I am nervous about getting group members who are lazy and are not motivated at all. See, I don’t mind getting shitty colleagues or group member once I am out there in the work force; at least you are getting paid to work with assholes. :P But I am going to keep a very optimistic attitude next semester regardless of the types of group members that I have. I believe that I am capable to fend for myself (and for them). I need an idea for an awesome web application though. Give me some ideas, please? ;)

Patience is Virtue
Despite trying hard to be “zen” this semester, I had failed miserably in trying to be patient. I noticed that I get increasingly temperamental and easily agitated towards the end of the semester. I yelled at two students because they unplugged my laptop without my permission when I was in the loo and make sarcastic or rude remarks at those who annoys me. I also ended up “lecturing” a group member for being incompetent since he cancelled an important meeting at the very last moment.

It is not that I am not trying my best. I do try to be patient and it seems to work during the beginning and the middle of the semester. Towards the end of the semester, everything started to get hectic and my tolerance level dropped down to 0. I’ll try harder next time.

Travel
I went to Portland, Oregon last month and it was a fun trip. I do believe that I tend to underestimate my own potential at times. I missed 4 days of classes and was freaking out that I won’t be able to do well in my next exam (I had an exam the next day after I arrived home. No time to see the professor for advise) and I had 2 programming assignments due. To my surprise, I managed to do well in my assignments and I did well for the exam despite preparing last minute at the airport.

The conference in Portland taught me a lot about the importance of having self confidence. It is not that I am not confident but I do have that occasional depressing moment where I will underestimate my own potential. According to the conference, this is pretty normal especially among young ladies. I will blame it on my period and my mood swings (PMS) :P

I plan to travel to Thailand next month. Hope I managed to get tickets!

Intense Craving for…
A hot guy in my bed ANIME! Gosh, I missed watching anime so much. For some weird reason, I missed watching Bleach. I thought I was over it since the story line is not consistent anymore and it is a bore but I guess I’ll try and watch a few recent episodes and check out some spoilers. Planning to watch other anime too. Give me suggestions on what anime to watch if you have one…

Love Life

Pretty much non-existent. I think I should get out more and be a bit more social. Recently, I saw a picture of my ex-boyfriend. He looked pretty ripped now. Why can’t he looked that way when we were dating?! And why did I dumped him again?! Looking good is always the best revenge. :P

WordPress Woes: WP_Options Table Crashed

Tagged:
2

My blog was down a few days ago. It says “Error Establishing a Database Connection”. So what did I do? I promised myself that I would document my little misery.

1. Navigate to WordPress WP-Admin
I know that I won’t be able to log in but I was curious so I decided to check out how the WP-login page would look like when my blog just crashed. Weird timing to be curious, isn’t it?

WordPress is sweet enough to tell me to define the WP_Debug on my wp-config.php file so that they can help me find out my main problem. Being curious is good. After defining WP_Debug on my wp-config.php file, I found out that my WP_Options table needs to be repair.

So, when you are receiving a white screen that says: “Error Establishing a Database Connection”, define the WP-Debug in your config file. Your WP_options table may or may not be the source of the problem. For this entry, I’ll just focus on the WP_Options table.

2. Repair WP_Options through PHPMyAdmin or WordPress
If I am not mistaken, WordPress will prompt you and ask for your permission to allow them to repair your database automatically. I can’t remember whether they did prompt me or not. Maybe they did and I probably repaired my table twice. What I did remember was navigating to my CPanel and repair the table through PHPMyAdmin. I think it is easier to repair it through WordPress and do it through WordPress if they prompt you.

3. You may or may not receive the blank screen of death
After repairing my database, I refreshed my site. WordPress blank screen of death. Nothing showed up. Just white all over! My wp-admin page is blank too. Oops! So, I checked my wp-config.php file. I screwed it up a bit when I was defining the WP-Debug line; it was missing the PHP closing tag: ?>

If missing a PHP closing tag is not the case for you, head over to HighTechDad and read through it. Try all options: rename plugins file, check wp-config.php and wp-settings.php

4. Check the number of records in WP-Options table
I tried to figure what causes my WP_Options table to crash by making a wild guess: my WP_Options table might be bloated by unnecessary records. I might have too many records in my WP_options table.

I am using the WP-DBManager plugin by Lester Chan. It tells me the number of record that I have for each of my tables. You can also do this via phpMyAdmin.

So, do you have more than 500 records in WP_Options table? No? Then skip the next point and go straight to point number 6. Yes? Continue reading!

5. Install The “Clean Options” Plugin
You need this in order to clean your WP_Options table and delete all the records that you are not currently using anymore. Why do you need to do this?

I activated and deactivated lots of plugins before on my blog. See, when you deactivate a plugin, it just stops the plugin from working but it doesn’t really delete their database records. Some of these unused plugin records are left in the WP_Options table. I am well aware of this before but I couldn’t be bothered to actually clean the unused records in my database.

The clean options plugin will help you find all the “orphaned records” for you. If you had deactivated a plugin before, I suggest you to use this plugin and clean up your database records. But I strongly suggest that you back up your database first before you do this. You do not need to delete ALL of the orphaned records. Sometimes, other records are mistakenly identified as “orphaned” when it is not. So, make sure to Google each of the record names before you delete it.

I had over 900 records before I used this plugin. I only have 296 records in my table now.

6. Identify the Problem
See, I am not even sure if the high number of records in my WP_Options table is the main reason why my table crashed. After researching more, high number or records in your table is one of the problem that can cause your table to crash. I read that bad plugins or bad themes can crash your table as well.

I am still researching more about this but if you know something about this, don’t hesitate to comment and I’ll add your suggestions up here too.