How to Automatically Link Twitter Usernames in WordPress

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 for the promotion of a website but 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. Previous, 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.

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 actually 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 creating your own plugin where you will paste the code listed below and activate to embed usernames and link to the corresponding profile automatically. There are also various ready-made plugins available which we would come to later.

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="//$2\&quot;" target="\&quot;_blank\&quot;" rel="\&quot;nofollow\&quot; noopener noreferrer">@$2</a>",$content);
return $twtreplace;
add_filter('the_content', 'twtreplace');
add_filter('comment_text', 'twtreplace');

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 Twitterlink 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’, ‘twtreplace’);

It is important to note that every time you mention ‘@’ at the beginning or 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 link as ‘@’ does not occur at the beginning.

How To Automatically Link Twitter Usernames In WordPress Using Third Party Plugin?
There are various plugins available which 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 slider and side tab option to show tweets. You can also show 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 in 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 and link your twitter account from the settings and fill 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.

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) and copy the URL from the address bar of the browser and pasting it, it will automatically get embedded in WordPress.

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 basically an expanded view of general tweet. It shows the images in a tweet and various other details.

Yoast SEO has the option to enable this functionality. Go to the Settings of the plugin and click on 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 shares exciting WordPress themes, plugins and other WordPress related news for our viewers. He also posts selected WordPress developers interviews from time to time.