How to Automatically Link Twitter Usernames in WordPress

Automatically Link Twitter

Twitter has become an important platform for the success of a website. There are so many users present on Twitter that you cannot ignore it. Even though Facebook is still the leader in the promotion of a website it is too much crowded to get enough exposure.

Therefore, Twitter has become a favorite platform for website owners to gather loyal followers and users and direct them to the website.

It is highly necessary to automatically link Twitter usernames In WordPress. Previously, Twitter introduced Twitter Anywhere API through which it was very easy to link Twitter usernames In WordPress, but it was discontinued in December 2012 due to overexploitation.

Automatically Link Twitter Usernames in WordPress

Automatically Link Twitter

But nothing remains stopped for anything and since Twitter is a regular part of website promotion and source of huge traffic, people have found out another way to automatically link Twitter usernames In WordPress. It is necessary because in today’s world, quoting tweets in posts is a popular trend.

If you do not know yet what it does, whenever you mention in your post with ‘@username’, it will link the Twitter profile of that person on your post so that users can go to his/her profile, read other tweets, and follow the person.

Furthermore, there are Twitter widgets that people use very often to showcase tweets directly in the post, and in such a scenario, it helps to show the authenticity of the post.

How To Automatically Link Twitter Usernames In WordPress Using Code?
There are various ways to link Twitter usernames In WordPress, but the best way out is to create your plugin where you will paste the code listed below and activate it to embed usernames and link to the corresponding profile automatically. There are also various ready-made plugins available which we would come to later.

AutomaticallyLinkTwitter 2

Create a file and name it something meaningful like twitterlink.php. Paste the following code inside it.

<!--?php &lt;br ?--> /*
Description: Automatically link Twitter usernames in WordPress
*/
function twtreplace($content) {
$twtreplace = preg_replace('/([^a-zA-Z0-9-_&amp;])@([0-9a-zA-Z_]+)/',"$1<a href="//twitter.com/$2\&quot;" target="\&quot;_blank\&quot;" rel="\&quot;nofollow\&quot; noopener noreferrer">@$2</a>",$content);
return $twtreplace;
}
add_filter('the_content', 'replace');
add_filter('comment_text', 'replace');

Save the file and then you have to connect to your web server through your FTP software and place the file in the plugins folder.

Then disconnect and go to Admin Dashboard’s left menu, hover over Plugins, and click on Installed Plugins. There you will find the Twitter link plugin. You need to activate it.

You would note that add_filter has the_content as a parameter. It means that automatically linking Twitter usernames In WordPress will only be possible for posts and pages.

If you want the same feature on your excerpt, you have to add another filter by adding the following line of code.
add_filter(‘the_excerpt’, ‘replace’);

It is important to note that every time you mention ‘@’ at the beginning of a word in your post or page, it will automatically link to the corresponding Twitter profile. But it does not mean that email addresses will get linked as ‘@’ does not occur at the beginning.

How To Automatically Link Twitter Usernames In WordPress Using Third Party Plugin?
There are various plugins available that you can use if you do not want to create your own using the method we mentioned above.

Any User Twitter Feed is a perfect plugin to achieve the same. It has various other features that you will certainly find useful.

You can embed timelines of users using their username and shortcode. You can search tweets with a keyword and add various visual effects which are not possible with the previous method we stated.

If you go for the premium version of the plugin, you can have a slider and side tab option to show tweets. You can also show the follow me button which is highly important to grow your followers and make your post visually appealing.

Different Ways You Can Integrate Twitter With Your Website?

Since you have given so much importance to Twitter, it is going to be somewhat necessary to have all the features of Twitter on your WordPress site.

It does not just end with by automatically linking username. There are the different ways you can integrate Twitter into WordPress.

Auto-Tweet New Blog Posts – If you to post a tweet every time you post a new article on your website, you can do so using the popular plugin WP to Twitter.

Install and activate the plugin link your Twitter account from the settings fill in all the required fields about how the tweet would look like and save changes.

Whenever you publish a post, you will be asked to tweet and you can write the tweet, and sometimes, you can choose not to tweet.

AutomaticallyLinkTwitter 3

Auto-Tweet Old Posts – It is important that you keep promoting your old posts as well for the website to run smoothly and not just based on new posts.

That way, you will get more traffic, and your posts will stay intact as far as ranking goes on a search engine. To promote old posts automatically, install and activate the plugin Revive Old Post and that is it.

You do not have to do anything more as all the old posts will get tweeted from time to time. Just go to Settings and set it up perfectly.

Embedding Tweets In WordPress – There are many scenarios when you would like to embed a Tweet on your post so that it looks authentic. It can be done by copying the URL of the tweet and pasting it into the post.

Just click on a tweet(yours or someone else’s) copy the URL from the address bar of the browser and paste it, it will automatically get embedded in WordPress.

AutomaticallyLinkTwitter 4

If you want to mention something specifically about the tweet, you can do so and use @username and it will automatically link Twitter usernames in WordPress as you have done the first method already.

Adding Twitter Cards – Rather than embedding just the tweet, some people like to add the entire Twitter card especially when their post consists of only one tweet.

A Twitter card is an expanded view of the general tweet. It shows the images in a tweet and various other details.

AutomaticallyLinkTwitter 5

Yoast SEO has the option to enable this functionality. Go to the Settings of the plugin click on the Social tab and then to the Twitter tab. Enable Add Twitter Card and select the default card.

About Sonnal S Sinha

Sonnal S SinhaSonnal S Sinha is a passionate writer as well as WordPress and WooCommerce rockstar who loves to share insights on various topics through his engaging blog posts. He runs a successful website design and digital marketing company. With 15+ years of experience in WordPress theme development, he strives to inform and inspire readers with his thought-provoking content. He helps thousands of small and medium businesses and startups create a unique online presence. Follow Sonnal S Sinha for your regular dose of knowledge and inspiration.