Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Kupası
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:

    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:

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

Açıklama: İçeriğimiz okuyucu desteklidir. Bu, bazı bağlantılarımıza tıklarsanız komisyon kazanabileceğimiz anlamına gelir. WPBeginner'ın nasıl finanse edildiğini, neden önemli olduğunu ve nasıl destek olabileceğinizi görün. İşte editoryal sürecimiz.

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.

Ultimate WordPress Araç Kiti

Araç setimize ÜCRETSİZ erişim sağlayın - her profesyonelin sahip olması gereken WordPress ile ilgili ürün ve kaynaklardan oluşan bir koleksiyon!

Reader Interactions

11 yorumBir Cevap Bırakın

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Aleks says

    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…

  3. Downhill_MC says

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

  4. Rarst says

    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.

Bir Cevap Bırakın

Yorum bırakmayı seçtiğiniz için teşekkür ederiz. Lütfen tüm yorumların yorum poli̇ti̇kasi uyarınca denetlendiğini ve e-posta adresinizin yayımlanmayacağını unutmayın. Ad alanında anahtar kelime KULLANMAYIN. Kişisel ve anlamlı bir sohbet edelim.