Automatically Link to Twitter Usernames in Content

Automatically Link to Twitter Usernames in Content : This is especially useful if you are running a website that focuses a lot on Twitter (probably a viral content site, etc.)

The following code snippet for functions.php converts all @ mentions in your content to their respective Twitter profiles.

Why dont I just use a plugin? Good question. The more plugins you have installed on your WordPress installation the slower your site loads. Remember search engines use your page load speed as a ranking factor. So if you want your SEO to be effective and your rankings increased on search engines, one must pay attention to SEO factors such as this. If your not comfortable with code or SEO we can help. Tap here.

For example, an @happy mention in your content will be converted to a link to the Twitter account “twitter.com/happy” (“happy” being the username):

function content_twitter_mention($content) {
return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>", $content);
}
add_filter('the_content', 'content_twitter_mention');   
add_filter('comment_text', 'content_twitter_mention');

Related Posts:

Choose your preferred blog CMS platform

WordPress Posts vs Pages

Introduction to JavaScript – Create a Variable: let

CSS, HTML, JAVASCRIPT, Online Compiler. Code on the go by replit.it

Introduction to JavaScript – Control Flow: if/else Statements

Show Post Thumbnails in RSS Feed – WordPress

Create a PayPal Donation Shortcode – WordPress

What is a Shortcode in WordPress?

What is a robot.txt file?

Formatting Text in WordPress Posts (Tiny MCE Advanced for WordPress)

Redirect New Registered Users to a Specific Page – WordPress

 

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.