Automatically Link Twitter Usernames In Content

If you mention people in your WordPress posts or link to your own Twitter profile in a post you can use the following code snippet to automatically asd a link around the Twitter username which will link to the Twitter profile.

This is a WordPress snippet that you can put into your theme functions.php file or create a plugin out of this code.

This function will search the content of the post for a single word after an @ symbol, for example if you put @paulund this will automatically be linked to the Twitter profile.

function content_twitter_mention($content) {
	return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>", $content);
}

add_filter('the_content', 'content_twitter_mention');   
add_filter('comment_text', 'content_twitter_mention');

Advertise here 50% Off

Comment