{"id":120005,"date":"2013-08-19T16:15:11","date_gmt":"2013-08-19T20:15:11","guid":{"rendered":"http:\/\/wpmu.org\/?p=120005"},"modified":"2013-09-05T01:00:10","modified_gmt":"2013-09-05T05:00:10","slug":"automatically-link-twitter-names-to-their-accounts","status":"publish","type":"post","link":"https:\/\/wpmudev.com\/blog\/automatically-link-twitter-names-to-their-accounts\/","title":{"rendered":"WordPress Tip: Automatically Link Twitter Names to Their Accounts"},"content":{"rendered":"<p>Do you find yourself referencing Twitter handles on a regular basis? If so, this quick trick will set your site up so that all you have to do is type in someone\u2019s Twitter name, and it will automatically be linked to their account.<\/p>\n<p>Take a look at the example below. All I did was type in @wpmudev and it linked itself to WPMU DEV\u2019s Twitter page.<\/p>\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\">\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\">\n<div class=\"image-grid cgrid-row\">\n<div class=\"cgrid-col cgrid-col-span-full\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-120823\" src=\"https:\/\/wpmudev.com\/blog\/wp-content\/uploads\/2013\/08\/twitter-link2.jpg\" alt=\"twitter-link2\" width=\"696\" height=\"263\" \/><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Simply place the following into your functions file. (Appearance &gt; Editor &gt; Theme Functions \u2013 functions.php) Either that or use a child theme or a <a href=\"https:\/\/wpmudev.com\/blog\/automate-the-creation-of-a-wordpress-functionality-plugin\/\" target=\"_blank\" rel=\"noopener\">functionality plugin<\/a>.<\/p>\n<p>{code type=php}<\/p>\n<p>\/* Automatically link Twitter usernames in WordPress*\/<br \/>\nfunction twtreplace($content) {<br \/>\n$twtreplace = preg_replace(&#8216;\/([^a-zA-Z0-9-_&amp;])@([0-9a-zA-Z_]+)\/&#8217;,&#8221;$1&lt;a href=\\&#8221;http:\/\/twitter.com\/$2\\&#8221; target=\\&#8221;_blank\\&#8221; rel=\\&#8221;nofollow\\&#8221;&gt;@$2&lt;\/a&gt;&#8221;,$content);<br \/>\nreturn $twtreplace;<br \/>\n}<br \/>\nadd_filter(&#8216;the_content&#8217;, &#8216;twtreplace&#8217;);<br \/>\nadd_filter(&#8216;comment_text&#8217;, &#8216;twtreplace&#8217;);<\/p>\n<p>Thanks to <a href=\"http:\/\/snipplr.com\/users\/ederwp\/\" target=\"_blank\">elderwp<\/a> for this tip.<\/p>\n<p>Photo credit: <a href=\"http:\/\/www.flickr.com\/photos\/eldh\/5858249526\/\" target=\"_blank\">Andreas Eldh<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This quick tip will let you type in a Twitter handle and have it linked up automatically.<\/p>\n","protected":false},"author":84404,"featured_media":120822,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"blog_reading_time":"","wds_primary_category":0,"wds_primary_tutorials_categories":0,"footnotes":""},"categories":[263],"tags":[982],"tutorials_categories":[],"class_list":["post-120005","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-social-media"],"_links":{"self":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/120005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/users\/84404"}],"replies":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/comments?post=120005"}],"version-history":[{"count":1,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/120005\/revisions"}],"predecessor-version":[{"id":216576,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/posts\/120005\/revisions\/216576"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media\/120822"}],"wp:attachment":[{"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/media?parent=120005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/categories?post=120005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tags?post=120005"},{"taxonomy":"tutorials_categories","embeddable":true,"href":"https:\/\/wpmudev.com\/blog\/wp-json\/wp\/v2\/tutorials_categories?post=120005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}