How to automatically link Twitter usernames to their profile in WordPress
Twitter is one of the most powerful and popular social media platforms on the web today, which means it is constantly changing, sometimes not always for the better. A long time ago Twitter launched the ‘Twitter Anywhere API’ which made it really easy to mention twitter usernames and automatically link them to the right profile. Unfortunately it was short lived and Twitter decided to retire and depreciate the API due to over exploitation. Luckily there are still other ways to automatically link Twitter usernames to their profile in your WordPress posts and pages, our favorite is through the use of a WordPress function. In this tutorial we will teach you how to automatically link Twitter usernames to their profile in WordPress without using a plugin.
Automatically link Twitter usernames to their profile via a WordPress Function
In order to automatically link Twitter usernames to their profile in WordPress, we will add the following PHP filters to the WordPress functions file. Simply open your functions.php file, then copy and paste the code below. After uploading your updated functions.php file all of your Twitter @ mentions added inside of the content editor will now link to their corresponding Twitter profile.
PHP
function twitter_name_replace($content) { $twitter_name_replace = 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 $twitter_name_replace; } add_filter('the_content', 'twitter_name_replace'); add_filter('comment_text', 'twitter_name_replace');
Example
Troubleshooting
If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.