Written on November 22nd, 2008 at 12:11 am by Darren Rowse
How I added the Twitter ID field to comments on Twitip.com
A couple of days ago I added the ability for those leaving comments on my TwiTip Twitter Tips blog to add their Twitter ID as well as their URL to comments. Since doing this I’ve been asked time and time again how we did it. In this guest post Sean Walberg (the guy who actually made it happen and who is my server admin on TwiTip) explains how he did it.
If you’ve been to Twitip.com in the past couple of days, you’ve probably noticed that commenters have Twitter IDs in their posts:

This was something Darren thought up while we were preparing to move his server, and asked me to put it in place. Twitip runs on WordPress, which is easily extended through plugins. Fortunately, someone wrote a plugin to take care of most of what’s needed to do this.
The steps we’ll follow here are:
- Get the plugin and activate it
- Configure the plugin for the new Twitter ID field
- Modify your theme to capture the information in your comment form
- Modify your theme to print the Twitter ID in the displayed comments
The first thing you’ll need is the Extra Comment Fields plugin. I used Version 1.2 Beta if you want a direct download link.
There’s one PHP file in there that you’ll put in your wp-content/plugins/ directory. When you go back to your WordPress Admin page, you’ll see the Extra Comment Fields plugin in the Inactive Plugins section of your Plugins page (off to the right of your screen, between Settings and Users). Activate this plugin.
The Extra Comment Fields plugin is tricky in that there are several references to the field name, and they all have to match (including the case). I chose “twitter” which should be easy enough to remember.
From the WordPress Admin screen, navigate to Settings, then Extra Comment Fields. (If you don’t see Extra Comment Fields in the list of plugins, go back a step and make sure you activated it)
Type “twitter” into the text box and click “Add Field”. Your screen should look like this:
This step makes the necessary changes to the database to store the comment. Heed the warning you see, if you delete this field, it removes the column in the database.
Next, go into your theme directory, it’s in wp-content/themes/. Darren runs Thesis, so we’re looking at wp-content/themes/thesis. Open up comments.php. Find the spot where the comment form is displayed by looking for comment_author_url.
You’ll want to add a new comment field with the name of “twitter”. This will depend a lot on your theme, but cutting and pasting goes a long way here. The big change, though, is that WordPress doesn’t keep this value in your cookie, so you can’t pre-populate this in the value attribute (in simpler terms, people who have commented before will have their name, email, and URL filled in, but not the Twitter ID). In Thesis, you’ll have:
else { // Otherwise, give your name to the doorman
?>
<p><input class="text_input" type="text" name="author" id="author" value="<?php echo $comment_author; ?
>" tabindex="1" /><label for="author"><?php _e('Name', 'thesis'); ?></label></p>
<p><input class="text_input" type="text" name="email" id="email" value="<?php echo $comment_author_emai
l; ?>" tabindex="2" /><label for="email"><?php _e('E-mail', 'thesis'); ?></label></p>
<p><input class="text_input" type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>"
tabindex="3" /><label for="url"><?php _e('Website', 'thesis'); ?></label></p>
<p><input class="text_input" type="text" name="twitter" id="twitter" value="" tabindex="4" /><label for
="twitter"><?php _e('Twitter id', 'thesis'); ?></label></p>
<?php
In the default theme, it’ll look like this:
<?php else : ?> <p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> /> <label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p> <p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> /> <label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p> <p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" /> <label for="url"><small>Website</small></label></p> <p><input type="text" name="twitter" id="twitter" size="22" tabindex="4" /> <label for="twitter"><small>Twitter ID</small></label></p> <?php endif; ?>
At this point load your site in your browser to make sure everything looks OK.
The last step is to print the Twitter ID alongside the comments. This is also done in comments.php. The comments are printed out in a loop which varies from theme to theme. In Thesis, you’ll start right after:
<?php thesis_hook_before_comment_meta(); thesis_comment_meta($comment_number); thesis_hook_after_comment_meta(); ?>
(It’s entirely possible there’s an easier way to do this in Thesis)
In the default theme, you’ll see the following, and you’ll want to start right before it.
<?php comment_text() ?>
Wherever you end up starting, insert the following code:
<?php if ($comment->extra_twitter) {
// Strip out the @ if they put it there because we're going to need to get rid of it for the url anyway
$extra_twitter = preg_replace("/^@/", "", $comment->extra_twitter);
$extra_twitter = htmlentities($extra_twitter); ?>
<a href="http://twitter.com/<?=$extra_twitter ?>">@<?=$extra_twitter?></a>
<?php } // extra_twitter ?>
The Extra Comment Fields plugin puts the data from the twitter field in a method of the $comment object called extra_twitter (and if you called your variable foo, it would be called extra_foo, and so forth). The above code checks to see if it’s been set (that is, someone submitted a Twitter ID), and if so, strips off any leading “@”. This allows people to enter their Twitter IDs both with and without the @. The code then calls the htmlentities function to strip out any funny stuff, and then print it out as a link. If you want to style the link with any CSS, change the HTML accordingly.
That’s it!
Sean Walberg is a network guy, freelance author, and systems administrator.




53 Responses to “How I added the Twitter ID field to comments on Twitip.com” - Add Yours
Ganesh
November 22nd, 2008 12:16 am
That’s a cool hack. Every since I saw the twitter id field on TwiTip, I’ve been curious about it. Its a great addition to a blog that talks about twitter. I think adding a twitter id field would be nice for some social blogs too.
Harish
November 22nd, 2008 12:36 am
Thats a great coding.The plugin is mush useful for all the twitter user who blog.And also useful to twitterers too.This sounds great and interesting to me.
bonoriau
November 22nd, 2008 12:47 am
This really interesting. I’m going to try immediately. thank you for the tips.
bonoriau
November 22nd, 2008 12:48 am
This really interesting. I’m going to try immediately. thank you for the tips. @ Bonoriau
Amy Nathan
November 22nd, 2008 12:57 am
Too complicated for me, but I’m saving the link for the future! I seem to now be the sounding board for all my newbie Twitter friends!
Michael Carnell
November 22nd, 2008 12:57 am
Thanks! Been waiting for that info. Now… wondering how much 2.7 might mess this up…
Elliot
November 22nd, 2008 12:57 am
Great tip!, another resource to keep the people connected who comment on the posts;))
Mike Nichols
November 22nd, 2008 1:06 am
Thanks for this great tip! I use Thesis, too, and will be adding this to my site soon. But I’ll be looking for ways to use the custom.css and custom_functions.php rather than editing core files, which is now discouraged in Thesis.
Lisa B @ simply His
November 22nd, 2008 1:10 am
It’s about time you posted the directions :D This is sweet! Thanks so much Darren and Sean — I use Thesis so I get to borrow all this code exactly!
Kim Woodbridge | (Anti) Social Development
November 22nd, 2008 1:26 am
I noticed the inclusion of the twitter ID on twitip. Thank you for providing the code. This will be So useful. :-)
Collin - Feed Flare
November 22nd, 2008 1:39 am
Someone should right a plug in just to do this. There are so many theme’s out there and it almost seems that every coder uses a different method of building a theme.
I use the citrus theme from UBD, and I love it but am not sure if doing what you said above will do the trick for me or not. I am willing to send you the theme if you want to take a look at it. Just let me know.
Sean
November 22nd, 2008 1:50 am
@Michael Carnell – The plugin apparently works in 2.7, at least according to the comments on the plugin page
@Mike Nichols – This was my first exposure to Thesis. There is a hook right above where the twitter id is printed, so I’m sure there is an easy way to print the comment in a separate function. However, I didn’t see any such thing for modifying the comment form itself. The CSS can be used to style the new option if you want, it has an ID
@Colin – I’m not sure a plugin could be written to do this because there are no core hooks/filters at that point. It’s fairly easy to set up a development WordPress environment, so just give it a shot on your local machine. It’s just a matter of finding out where the stuff is printed (look at the generated source and then do a search in the code) and trying it out.
Styletime
November 22nd, 2008 1:58 am
Added! Thanks for that tip!
Styletime
November 22nd, 2008 2:06 am
Done but for some reason I have a ‘ appended to the end of the Twitter username? ;(
kantush
November 22nd, 2008 2:26 am
Would you be able to do this if you’re using comments plugin such as intense debate? I don’t think so, but I’ll still give it a try.
Funky Space Monkey
November 22nd, 2008 2:28 am
thank you for this tut!!!! this rocks. works like a charm, and easy to do. needed a little tweaking for me, but it got me there :D:D:D thank you once again
Andy Bailey
November 22nd, 2008 2:33 am
This might be something I could write a plugin for, no form editing required I think.
I’ll see if I can give it a go later and if it continues to be as easy as I think it might be then it could be done by the end of the weekend.
I’ll let you know!
Jake
November 22nd, 2008 3:10 am
Darren/Sean – thanks for posting this. For one it is nice to see the “inner workings” and also I was looking into implementing that on my couple of blogs. Gives me a better direction to work on.
Thanks and great post!
Matt Gio
November 22nd, 2008 3:20 am
Interesting. I would have never thought to add that. What a brilliant idea.
Rahul
November 22nd, 2008 4:23 am
Quite a cool idea. Humm…
gosh now do i have to play with this <?php tags? not again…
Richard McKay
November 22nd, 2008 5:24 am
Like this idea – simple but valuable.
Cheers.
@holeinhiseye
Kevin Thompson
November 22nd, 2008 7:43 am
Wow. Thank you so much. This was easy to do with the step by step instructions and a value added that will continue to produce time and time again with each new comment. Thank you for the idea and willingness to share.
Cristhian Bedon
November 22nd, 2008 7:46 am
Nice mod, I like it, it a new way to network yourself apart from the regular site.
Andy Bailey
November 22nd, 2008 8:42 am
I have a working version of my plugin on my local site which does all above automatically. only need to add one line to the comments.php for where you want the link to be output.
should be finished tomorrow!
Andy Bailey
November 22nd, 2008 12:01 pm
ok, plugin finished.
easy install, one line to add to comments.php to show twitter id or link to users twitter page
you can see it working here
http://www.fiddyp.co.uk/i-just-voted-for-commentluv-in-the-mashable-web-awards/#comment-35080
I called it WP-Twitip-ID :-)
Jon Griffith
November 22nd, 2008 2:49 pm
Nice…greatly appreciated.
Takumi_86
November 23rd, 2008 12:54 am
Good tips but unfortunately this tips wont worked on Blogspot, is there a way to put this on blogspot?
Otto
November 23rd, 2008 5:28 am
You may not have known this, but if you have a user’s email address, then you can retrieve their Twitter ID via the Twitter API. You can either do this in the backend with an XML request, or in the browser with a JSON request.
It’s actually simple enough to do, just pull this URL:
http://twitter.com/users/show.xml?email=test@example.com
Look closely at the returned “Screen Name” field.
If you prefer JSON data, then hit this URL instead:
http://twitter.com/users/show.json?email=test@example.com
Voila. Why have users do the grunt work of typing in their Twitter info when you can just get it straight from Twitter?
Bob
November 23rd, 2008 7:42 am
Check out Sugarrae’s Social Profiles plugin for WordPress.
http://www.sugarrae.com/wordpress/social-profiles/
It allows you to add social network profile links to the comments of your registered users as specified by the user on their profile page. Stuff like flickr, Stumbleupon, Digg, and of course Twitter.
Eric Tan
November 23rd, 2008 10:41 am
Can’t get it to work… oh well…
Rosario
November 23rd, 2008 10:41 pm
Oh my ! Wonderful post . Thank U
Brad Hart @ A DoFollow Blog
November 24th, 2008 2:54 am
Thanks for explaining your approach. I find it very validating because it is the same approach I came up with the day I saw it appear on Twitip. I have now implemented it on several blogs and for anyone who wants to use this approach to expand their twitter network I have created a section on my link directory just for blogs that give a twitter ID field
http://wtfarewegoingtodonow.com/link-directory/
One quick question Darren have you found a way for logged in folks such as admin and others to use the same field info I can come up with a couple of plugins that allow adding fields to user profile, but I can’t seem to align the same field names. I hope some coding genius is working on a plugin to address this issue. I would like to offer it to my registered users as well as casual comenters.
Dan
November 24th, 2008 7:49 am
I tried twitter this and it failed. It needs lots of work and configuration which defeats it as a plug-in. A plug-in is something ready to go. You can spend a whole afternoon tinkering with the code and it still does not works well with twitter.
Hope someone comes up with better tool.
Rachel Levy
November 24th, 2008 2:03 pm
Great post! And, great idea to add twitter id to blog comments (but it’s not active here!)
@bostonmarketer
Sire
November 24th, 2008 5:28 pm
@-Dan…Andy Bailey did, look up a couple of comments.
Peter van Veen
November 24th, 2008 9:50 pm
Thanks a lot for this mod Darren&Sean! i’m going to implement this asap.
@PetervanVeen
dedmoroz
November 25th, 2008 1:19 am
Interesting. Thank you for information! =)
Jason Van Orden
November 25th, 2008 5:39 am
Very cool tweak. Thanks for sharing this. I put it up in a jiffy. One thing I would add to this is a nofollow in the Twitter ID link that is displayed so you don’t dump all your Google juice over to Twitter. This is done by adding the rel=”nofollow” tag to the link.
Jason Van Orden
November 25th, 2008 6:51 am
I just discovered one problem with this. It works great other than it doesn’t display my own Twitter ID as the author of the post. Since I am already logged in, I don’t have a place to enter my Twitter ID and there is no field for me to enter my ID when I comment.
That said, I’ll probably just put a quick check in the code to see if it is my comment and if so, display my Twitter ID.
nicholi
November 25th, 2008 9:48 am
This seems interesting but has quite a few steps to follow even with the plugin. Shouldn’t the plugin do all this stuff?
Paul @ Green Pepper
November 26th, 2008 2:35 am
Fantastic! Thanks Sean. I’m using Thesis too so should make it easier to follow your instructions. Will give it a go. Many thanks.
Ari Herzog
November 29th, 2008 5:25 pm
I was tweaking my own Thesis code to do this very issue when I thought to myself, “I wonder if someone published a guide?” recalling Twitip.com used it.
Thanks for the hook help (a concept I’m still figuring out) and I’d never have thought of activating a new plugin.
All set now by clicking my name above.
Young
December 4th, 2008 2:38 am
Cool.I will try it out soon.
Chaitanya Patel
December 4th, 2008 6:18 pm
Lovely find, would like to see if bloggers will accept it or not?
Young
December 5th, 2008 2:40 am
I have tried the way, and it workable but there are still two problems:
1. there is border on the field area, but my theme has not.
2. the background are white, but my theme is green.
By the way, my blog use UBD Citrus Theme, so do you know how to settle the above two problems?
Hans Looman
December 24th, 2008 10:24 am
Thanks for the great manual, works ok. One other thing that may be improved is a hack that actually let’s one edit the twitter ID because you will run into people that enter mistakes.
(Just had one on the very first comment entered :-)
Other than that: kuddos!
@hlooman
Robin Sampson
January 13th, 2009 1:01 am
I followed directions but must have missed something. Won’t work for me?
Ben
January 21st, 2009 5:38 am
Otto: Thanks for the hint, made me look into the Twitter API. I hacked together a solution based on your idea:
Automatically add a Twitter profile link in the comments.
Hope this helps!
Jason
January 24th, 2009 11:34 pm
do you have similar plugin or hack for blogspot?
my site is http://itblood.blogspot.com and i wish to implement this.
Tracy Green
April 4th, 2009 2:35 am
How woulde google make money from twitter…adding ads would not seem to work.
Steve Patterson
May 26th, 2009 9:56 am
@friendlyknoxguy I tried that and it didn’t work for me. It won’t show up on the page when it loads. It shows the form to enter it and shows up on the wp-admin to edit comments. What do I need to do? Can anyone help me?
Steve Patterson
May 26th, 2009 10:00 am
sorry for 2nd comment, forgot to subscribe to replies to this post. Thanks for this entry btw.
Tracy
June 10th, 2009 5:54 am
This hasn’t worked for me either. I will keep on trying though!
Comments will be closed off on this post 90 days after it is published. Apologies to those this impacts but it's a regrettable and temporary measure to combat a growing comment spam problem. See our most recent posts where you can comment here.