Skip to content

How To Automatically Link Twitter Usernames in WordPress

Twitter is getting huge everyday and almost all huge companies are using it to promote and tout their content. Since we often mention various Twitter usernames in our posts and other places, we found a way to automatically link Twitter usernames in WordPress.

This snippet comes from folks at WPBeginner. Just add following snippet to your current theme’s functions.php file:

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

add_filter('the_content', 'twtreplace');

Above code will do the job. The above code will automatically link to Twitter usernames in WordPress posts. You can also enable this feature in your comments and excerpts by adding following snippet:

add_filter('comment_text', 'twtreplace');

add_filter('the_excerpt', 'twtreplace');

Leave a Reply

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