WP – Seguidores no Twitter sem plugins
Wednesday, 29 April 2009 @ 11:56h - - 1 Comentário
Categorized under: Dicas, Geral, Tutoriais
Veja como é simples mostrar o número de seguidores (followers) no Twitter em forma de texto no seu blog, sem a necessidade de instalar nenhum plugin.
Para ver um exemplo de como ficaria, basta dar uma olhada no perfil ao lado em "twitter updates".
Abra o arquivo functions.php do seu tema no WP e cole o seguinte código:
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;
}
Feito isso, salve o arquivo e cole o código abaixo em algum lugar do seu layout onde você quer que sejam exibidos o contador de seguidores:
<?php echo getFollowers("SEULOGIN"
." seguidores"; ?>
Lembre-se de trocar SEULOGIN pelo seu login (nome de usuário) no Twitter.
Não é necessário informar a senha!
Você pode também customizar o texto entre aspas pelo que você quiser, ou até mesmo adicionar um link ao texto exibido.
Daniele, 25 anos. 20/02, pisciana 
Moro desde 2004 em Uithoorn, na Holanda




















New blog post: Seguidores no Twitter sem plugins http://tinyurl.com/d8lmfc