WordPress Tip: Automatically Link Twitter Names to Their Accounts

Do you find yourself referencing Twitter handles on a regular basis? If so, this quick trick will set your site up so that all you have to do is type in someone’s Twitter name, and it will automatically be linked to their account.

Take a look at the example below. All I did was type in @wpmudev and it linked itself to WPMU DEV’s Twitter page.

twitter-link2

Simply place the following into your functions file. (Appearance > Editor > Theme Functions – functions.php) Either that or use a child theme or a functionality plugin.

1
2
3
4
5
6
7
/* Automatically link Twitter usernames in WordPress*/
function twtreplace($content) {
$twtreplace = 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 $twtreplace;
}
add_filter('the_content', 'twtreplace');
add_filter('comment_text', 'twtreplace');

Thanks to elderwp for this tip.

Photo credit: Andreas Eldh

Tags

Comments (3)

Participate