How to Link Twitter Usernames automatically in WordPress Blogs

TCS-Twitlinks Plugin

TCS-Twitlinks Plugin

Twitter has decided to retire from Twitter Anywhere API from December 6th, 2012. If you don’t really know about Twitter Anywhere API, then no problem this article will help you to make your own plugin to show twitter usernames and have them be automatically linked to the right profile.

Until now, Twitter used to take all these credit to make it easy for us. Now, as Twitter is on way to retire you may not want to rely on other third-party script, so here is a short and simple plugin to take care of the job previously handled by Twitter Anywhere API.

In this article, I will show you how to automatically link twitter usernames in WordPress when you mention it after the @ sign like so: @tcsweb .

All you have to do is open a blank .php file and call it tcs-twitlinks.php. Then copy the code below and save it in there. Upload the file into your plugins folder, and simply activate the plugin. You can use any PHP editiors for this, FYKI I used Adobe Dreamweaver CS5.

<?php
/*
Plugin Name: TCS Linkify Twitter Usernames
Description: Automatically link Twitter usernames in WordPress
Author: Luzan Baral
Author URL: http://www.thecomputerstudents.com
*/
function puttwtr($content) {
$puttwtr=preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
return $puttwtr;
}
add_filter('the_content', 'puttwtr');
?>

Here, we are using it on our single posts and pages, so we only have the filter for the_content. You can always extend this feature to excerpts as well by adding the following line:

add_filter('the_excerpt', 'puttwtr');

This does not mean that it is the only way to link twitter usernames automatically in WordPress blog. You may find many of similar kinds, but I assure that this is the fastest and most effective solution in my opinion.
If you find some bugs while doing this tricks in your blog please leave a comment. I will surely find a solution for that. Follow me in Twitter @nazulb

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>