Automatically link Twitter usernames in WordPress


SUBMITTED BY: Guest

DATE: Dec. 26, 2013, 6:25 a.m.

FORMAT: PHP

SIZE: 571 Bytes

HITS: 856

  1. If you’re often quoting Twitter users on your blog, what about automatically link twitter usernames? Here is a nice regular expression to do so. Simply add this code to your functions.php file for it to work.
  2. function twtreplace($content) {
  3. $twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content);
  4. return $twtreplace;
  5. }
  6. add_filter('the_content', 'twtreplace');
  7. add_filter('comment_text', 'twtreplace');

comments powered by Disqus