玩WordPress » 技巧方法 » 2009 » 04 » 16 » 在WordPress上展示嘀咕跟随人数

在WordPress上展示嘀咕跟随人数

在WordPress上展示嘀咕跟随人数

嘀咕(@bingu)是近来比较火的一个microblog站。嘀咕的优势在我看来,是可以快捷地在手机上用火兔拍照然后上传,而不用那该死的彩信来上传图片。而火兔的实现当然是离不开API的开放。既然有API,那我们也可以很容易地在自己的blog上展示嘀咕有多少人跟随。

效果请看冰古blog的侧边栏。

<?php
/*
Plugin Name: Microblog follower counter
Plugin URI: http://wanwp.com/tips-tricks/display-digu-followers-on-wordpress/
Description: Display your microblogs follower number.
Author: 冰古
Version: 1.0
Author URI: http://bingu.net
License: GNU General Public License 2.0 http://www.gnu.org/licenses/gpl.html
*/

$counter_options = get_option('counter_options');
if (!$counter_options)
        $counter_options = array(
                                'digu' => array(
                                    'name' => 'bingu', // change this to your digu username
                                    'last_time' => '',
                                    'followers' => '',
                                    )
                                );
/**
 *
 * @global array $counter_options
 * @param int $cache_time
 * @return <type>
 */
function digu_followers($cache_time = 86400) {
    global $counter_options;

    if (!$counter_options['digu']['name']){ // if username not available, return
        return ;
    }else{
        // username only allow contain underlines, numberics and letters, and length is 6?-12
        $digu_name = preg_replace('/[^_0-9a-z]/iu', '', $counter_options['digu']['name']);

        if ($digu_name == '' || isset($digu_name{12}))
            return ;

        $now_time = time();

        if ($counter_options['digu']['last_time']){
            if (($counter_options['digu']['last_time'] + $cache_time) > $now_time){
                return $counter_options['digu']['followers'];
            }
        }

        $api_pattern = 'http://api.digu.com/statuses/followers_count/%s.json';
        $api_link = sprintf($api_pattern, $digu_name);

        require_once(ABSPATH . WPINC . '/class-snoopy.php');
        $snoopy = new Snoopy;
        $snoopy->fetch($api_link);
        if (function_exists(json_decode)){
            $followers = json_decode($snoopy->results, true);
            $followers = $followers['followersCount'];
        }else{
            preg_match('/"followersCount":"([0-9]+)"/i', $snoopy->results, $followers);
            $followers = $followers[1];
        }
        if (!(int)$followers) $followers = $counter_options['digu']['followers'];
        $counter_options = array(
                                'digu' => array(
                                    'name' => $digu_name,
                                    'last_time' => $now_time,
                                    'followers' => $followers,
                                    )
                                );

        update_option('counter_options', $counter_options);

        return $followers;
    }
}

代码如上。因为最近比较忙,我没有在后台添加选项页,在使用前你需要修改$counter_options中的name为你的嘀咕用户名。然后在主题中添加类似的代码:

<img src="http://digu.com/favicon.ico" alt="嘀咕" width="16" height="16" /><a href="http://digu.com/bingu">冰古有<span style="font-size:1.2em;color:blue;"><?php echo digu_followers();?></span>位跟随者</a>

效果请看冰古blog的侧边栏。

6 Responses to “在WordPress上展示嘀咕跟随人数”

  1. guest says:

    看看去。蛮有意思, 跟随者 念起来像明星呢。哈哈

  2. 西风 says:

    哟 不错哟

  3. 博译论 says:

    不错,我也要在博客上帮你宣传一下

  4. […] 2、嘀咕 用嘀咕秀在博客上秀你的嘀咕。也可以使用这个方法在你的博客中显示嘀咕更新。也可以在博客上秀“嘀咕跟随人数”。 […]