Blogging and social media go hand-in-hand. Sometimes, on the fly, you need to add someone’s Twitter handle, and then grab their link. However, what if you don’t have time for that? What if you could link Twitter usernames in WordPress without a plugin? Wouldn’t that save a lot of time?
Well, you can, and this is how.
How to Link Twitter Usernames in WordPress Without a Plugin
This method is to display the at sign symbol @ and the username you want to link. This snippet of code is short and does the job for you.
It’s super simple.
Step 1. Copy the code highlighted in black.
/*
Autolink Twitter usernames in WordPress
*/
function twtreplace($content) {
$twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1@$2",$content);
return $twtreplace;
}
add_filter('the_content', 'twtreplace');
add_filter('comment_text', 'twtreplace');
Step 2. Paste the code to your theme’s functions.php file.
Step 3. Just type the at sign and the Twitter username when you’re creating content on a post or page. Here’s it in action: @blondishnet
This method should work in the Gutenberg content block editor, or Classic editor. For those that like to do people roundups, or company roundups, this will save a lot of time.
Leave a Reply