Want to know what WordPress Theme a site is using? Check Out Now!

How to Link Twitter Usernames in WordPress Posts & Comments Automatically?

Do you want to link Twitter usernames in WordPress posts and comments automatically?

There are multiple scenarios when we need to mention the Twitter accounts of people and brands. And we have to search for their profiles and link them to the mention.

You can automate the linking process of a Twitter mention by using the below code snippet.

/**
 * Automatically Link Twitter Usernames in WordPress Posts & Comments
 *
 * @param string $content
 * @return string
 */
function maverick_link_twitter_usernames_wordpress($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', 'maverick_link_twitter_usernames_wordpress');   
add_filter('comment_text', 'maverick_link_twitter_usernames_wordpress');

This snippet was created by Paul Underwood.

Leave a Reply

Your email address will not be published. Required fields are marked *