Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPress’te Twitter Takipçi Sayısı ve Daha Fazlası Nasıl Görüntülenir?

Daha önce Rarst tarafından katkıda bulunulan twitter takipçi sayısını görüntülemenizi sağlayan bir kod hakkında yazmıştık. Bu yazıda, WordPress’te twitter takipçi sayısını görüntülemenizi sağlayan daha gelişmiş ve daha zarif bir kodu paylaşacağız. Bu kod da yine Rarst tarafından hazırlanmıştır.

Özellikler

Bu fonksiyon takipçi sayısı ile sınırlı değildir. Twitter users/show API yöntemi tarafından döndürülen herhangi bir iç içe olmayan değeri getirebilir.

İki seviyeli önbelleğe sahiptir:

  • Sorgulanan değerler, WP seçenekleri kullanılarak $aralık saniye için veritabanında dizi olarak saklanır;
  • API yanıtları bellekte saklanır, böylece çok sayıda API isteği oluşturmadan istediğiniz sayıda alanı sorgulayabilirsiniz.

Bu, API limitini tüketme endişesi olmadan değerleri çarpmak ve aynı zamanda kullanıcıları çarpmak için güvenli olmalıdır.

Öğretici

İlk olarak temanızın functions.php dosyasını açın ve aşağıdaki kodu ekleyin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function rarst_twitter_user( $username, $field, $display = false ) {
$interval = 3600;
$cache = get_option('rarst_twitter_user');
$url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
 
if ( false == $cache )
$cache = array();
 
// if first time request add placeholder and force update
if ( !isset( $cache[$username][$field] ) ) {
$cache[$username][$field] = NULL;
$cache[$username]['lastcheck'] = 0;
}
 
// if outdated
if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
 
// holds decoded JSON data in memory
static $memorycache;
 
if ( isset($memorycache[$username]) ) {
$data = $memorycache[$username];
}
else {
$result = wp_remote_retrieve_body(wp_remote_request($url));
$data = json_decode( $result );
if ( is_object($data) )
$memorycache[$username] = $data;
}
 
if ( is_object($data) ) {
// update all fields, known to be requested
foreach ($cache[$username] as $key => $value)
if( isset($data->$key) )
$cache[$username][$key] = $data->$key;
 
$cache[$username]['lastcheck'] = time();
}
else {
$cache[$username]['lastcheck'] = time()+60;
}
 
update_option( 'rarst_twitter_user', $cache );
}
 
if ( false != $display )
echo $cache[$username][$field];
return $cache[$username][$field];
}

Kullanım

Fonksiyonu yapıştırdıktan sonra, artık kodu istediğiniz herhangi bir WordPress şablon dosyasında kullanabilirsiniz. Aşağıdaki kodu yapıştırmanız yeterlidir:

1
2
3
echo rarst_twitter_user('wpbeginner', 'name').' has '.
rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';

Yukarıdaki kod aşağıdaki gibi bir şey gösterecektir:

WPBeginner’ın 1300 güncellemeden sonra 5846 takipçisi var.

Kaynak: Rarst

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

10 yorumLeave a Reply

  1. Aleks

    Someone explain where I put in my own twitter username please? Cause I really cant see where… And I cant get it to work either… No matter what.. This just wont give me username, not even leaving it completly basic as it stands right now, I get no response on username what so ever… it just displays: “has followers after updates”…
     
    That’s it.. nothing else…

    • wpbeginner

      In the usage section, replace “wpbeginner” with your twitter name.

  2. Downhill_MC

    this is a great code. i wonder if there is a possibility to create a function for embedding in text (something like ). downhill_mc

  3. Sandeep

    will you please tell me how should i add these codes in my custom twitter image as you shows us in your example.

    • Editorial Staff

      You would have to use CSS.. unfortunately we are not writing tutorials for those on this site.

      Admin

  4. Ozh

    (You should display syntax hilited and cleanly formatted code, really)

    • Editorial Staff

      Thanks for the correction. We really appreciate you providing the awesome snippets for everyone to use. Your hard work is really appreciated.

      Admin

  5. Rarst

    Glad you found it useful. :) Old snippet still works but it got kinda outdated and spread around a lot – it was getting hard to answer questions and correct outdated parts all the time.

    This one is slightly more bulky, but it has much extended functionality for showing more data and for different usernames at the same time.

    I also intend to maintain it more properly so feedback and suggestions are welcome on its page at my blog.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.