{"id":588,"date":"2010-12-28T23:46:37","date_gmt":"2010-12-28T21:46:37","guid":{"rendered":"http:\/\/www.kavdesign.net\/blog\/?p=588"},"modified":"2010-12-29T20:51:26","modified_gmt":"2010-12-29T18:51:26","slug":"how-to-add-the-number-of-twitter-followers-into-your-site","status":"publish","type":"post","link":"https:\/\/www.kavdesign.net\/blog\/coding\/how-to-add-the-number-of-twitter-followers-into-your-site\/","title":{"rendered":"How to add the number of Twitter followers into your site?"},"content":{"rendered":"<p>The first thing to do is to paste the following php functions on the <em>functions.php<\/em> file from your WordPress blog theme:<br \/>\n<!--more--><\/p>\n<pre>function string_getInsertedString($long_string,$short_string,$is_html=false){\r\n  if($short_string&gt;=strlen($long_string))return false;\r\n  $insertion_length=strlen($long_string)-strlen($short_string);\r\n  for($i=0;$i&lt;strlen($short_string);++$i){\r\n    if($long_string[$i]!=$short_string[$i])break;\r\n  }\r\n  $inserted_string=substr($long_string,$i,$insertion_length);\r\n  if($is_html &amp;&amp; $inserted_string[$insertion_length-1]=='&lt;'){\r\n    $inserted_string='&lt;'.substr($inserted_string,0,$insertion_length-1);\r\n  }\r\n  return $inserted_string;\r\n}\r\n\r\nfunction DOMElement_getOuterHTML($document,$element){\r\n  $html=$document-&gt;saveHTML();\r\n  $element-&gt;parentNode-&gt;removeChild($element);\r\n  $html2=$document-&gt;saveHTML();\r\n  return string_getInsertedString($html,$html2,true);\r\n}\r\n\r\nfunction getFollowers($username){\r\n  $x = file_get_contents(\"http:\/\/twitter.com\/\".$username);\r\n  $doc = new DomDocument;\r\n  @$doc-&gt;loadHTML($x);\r\n  $ele = $doc-&gt;getElementById('follower_count');\r\n  $innerHTML=preg_replace('\/^&lt;[^&gt;]*&gt;(.*)&lt;[^&gt;]*&gt;$\/',\"\\\\1\",DOMElement_getOuterHTML($doc,$ele));\r\n  return $innerHTML;\r\n}\r\n<\/pre>\n<p>Then, simply paste the following anywhere on your theme files. Just replace my username with yours.<\/p>\n<pre>&lt;?php echo getFollowers(\"catswhocode\").\" followers\"; ?&gt;<\/pre>\n<p>If you enjoyed this article, please consider sharing it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first thing to do is to paste the following php functions on the functions.php file from your WordPress blog theme:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[9,82],"tags":[153,152,151,418],"class_list":["post-588","post","type-post","status-publish","format-standard","hentry","category-coding","category-wordpress","tag-code","tag-followers","tag-twitter","tag-wordpress","entry"],"_links":{"self":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/comments?post=588"}],"version-history":[{"count":4,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/posts\/588\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/media?parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/categories?post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kavdesign.net\/blog\/wp-json\/wp\/v2\/tags?post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}