Site icon CodeRevolution Knowledge Base

Automatically link Twitter usernames in WordPress – CodeRevolution

Are you using Twitter a lot? Today’s recipe is a cool piece of code to automatically link Twitter usernames on your posts, pages, and comments.

Paste the code below into your functions.php file:

 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'); 

Once you saved the file all twitter usernames in posts and comments will automatically be linked to their Twitter profiles. Usernames have to be written under the form @username.

Thanks to ederwp for the tip!

Exit mobile version