X

How To Automatically Convert Twitter Usernames in Posts Into Links

Snippets by IsItWP

Do you want to automatically convert Twitter usernames in posts into links? This snippet will look for text like @isitwp, and will wrap the username text with an anchor tag to make it a link like this: @isitwp

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

This snippet was created by Paul Underwood.

If you liked this code snippet, please consider checking out: 12 Best WordPress Social Media Plugins.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!