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