ブログにtwitterのフォロー数を表示いたい【WPハック】


消失させてしまった前のブログの記事を拾ってきましたので、再アップしてみる。
自分のtwitterをブログに表示している方も多いと思いますが、フォロー数も表示したい!時のハック。
プラグインを使えば簡単でしょうけど、あえてコードを書く方法。

Follow Me!

Follow Me!


お使いのテーマからfunctions.phpに下記コードを記述します。

function string_getInsertedString($long_string,$short_string,$is_html=false){
  if($short_string>=strlen($long_string))return false;
  $insertion_length=strlen($long_string)-strlen($short_string);
  for($i=0;$i<strlen($short_string);++$i){
    if($long_string[$i]!=$short_string[$i])break;
  }
  $inserted_string=substr($long_string,$i,$insertion_length);
  if($is_html && $inserted_string[$insertion_length-1]=='<'){
    $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
  }
  return $inserted_string;
}

function DOMElement_getOuterHTML($document,$element){
  $html=$document->saveHTML();
  $element->parentNode->removeChild($element);
  $html2=$document->saveHTML();
  return string_getInsertedString($html,$html2,true);
}

function getFollowers($username){
  $x = file_get_contents("http://twitter.com/".$username);
  $doc = new DomDocument;
  @$doc->loadHTML($x);
  $ele = $doc->getElementById('follower_count');
  $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
  return $innerHTML;
}

twitterのフォロー数を表示させたい所に下記ショートコードを書く。自分のTwitter名(アカウント)を忘れないで!

<?php echo getFollowers("your twitter name")." followers"; ?>

終了〜。

ネタもと:WpRecipesに「Display the total number of your Twitter followers on your WordPress blog


Facebook comments:

    One Response so far.

Leave a Reply

※コメントが認証されるまで、コメントは反映されません。






ポートフォリオを見る