WordPress comment hacks: User-only comments, restrict by user age and more
In a recent promo for Android and Me, we created a bunch of giveaways, each with a specific limitation on entry. We had 25 days worth of prizes, and each day held a different challenge. We used comments as the main way to enter, and on certain days we wanted to restrict entry to certain groups of users.
Some days we only wanted users who had added their Twitter account to the profile (a piece of user meta), some days we wanted only members of a certain age to be eligible (a piece of user meta compared to a set value), and overall, we wanted all comments to be from registered users only (private comments on certain WordPress posts).
To accomplish all these things, I came up with the simple hack of creating a slew of conditional checks that surround comment_form(). If you’re not familiar, it’s a function that outputs the comment form itself and is usually found near the bottom of comments.php. It’s a pretty easy way to control who is commenting, taking away the form. Take a peek a the heavily commented code below (no pun intended) and join me after for some play by play.
Jeeeez that’s a scary wall of text! Ok, not really, but compared to just the single line comment_form() we started with it looks pretty major. What we’re doing is actually pretty simple. Anytime the template would normally call comment_form, we’re getting in the way and checking some settings first.
What settings? Things we set on a post per post (or page) basis, using custom fields. If you’d like to turn on user-only comments, set a custom field ‘private_comments’ with any value (I usually just go with ‘true’). If you’d like to restrict comments based on whether or not a user has a certain meta field, on your post set a custom field with the key ‘meta_lock’ and a value matching whatever user meta you’d like to filter by. This can be a user’s real name, website link, bio, etc. It’s a great way to push your users to fill out certain fields of their profile.
One of the funnest challenges we did was based on user age, something you’ll see in the snippet above as age_lock. To use it, simply set a custom field on your post with a YYYY-MM-DD format (one year before today’s date, for example). When comments.php is rendered, it’ll compare the date the user registered with the date you’ve entered in the field ‘age_lock’.
These few examples are really just to get the juices flowing, and to introduce you to the simple hack of conditionally displaying the comment form. There are an infinite number of things you could check or compare, so if you explore and find something killer make sure you come back and share it with us.
So far, I think my favorite use of this technique was tracking user tweets with the service Rowfeeder, which I then used to export a full list of Twitter usernames. Since our accounts let users link their Twitter handle, I was able to check which WordPress accounts had successfully Tweeted and then conditionally allow them to comment. Total score!
DATE1st February, 2012