Tôi tin là bạn đã từng nhìn thấy ở một số blog có cách hiển thị số Feedburner Subscriber và Twitter Follower bằng định dạng text. Ví dụ thực tế thì bạn có thể xem ở Blog Việt hoặc ngay tại blog này của Izdesignz. Thật ra thì chúng ta vẫn có thể sử dụng chức năng FeedCount của Feedburner hay
Twitter Counter để hiển thị tổng số subscriber và follower bằng định dạng hình ảnh nhưng tôi thấy cách này không hay lắm, vì nó không thể tùy chỉnh theo ý thích của mình được. Ở bài viết trước tôi cũng đã có giới thiệu một cách để
Hiển Thị Tổng Số Twitter Followers Bằng Định Dạng Text, hôm nay tôi sẽ hướng dẫn cho các bạn một cách khác để có thể hiển thị cả tổng số subcriber và follower bằng định dạng text, nó sẽ giúp bạn linh động hơn trong việc thiết kế, có thể tùy chỉnh hiển thị theo ý thích của mình.
Bước 1 : Chỉnh sửa file functions.php
Các công việc chúng ta sẽ thực hiện :
- Thêm 3 functions vào file functions.php trong theme của bạn.
- Function đầu tiên là function cURL, bạn cần có nó để hai function sau hoạt động được tốt hơn.
- Function thứ hai được dùng để lấy số Feedburner Subscriber.
- Function thứ ba để lấy số Twitter Followers.
- Cuối cùng là chỉnh sửa những thứ cần thiết như : URL của blog, tên feed trên Feedburner và Twitter Username.
Function: cURL
02 | $ch = curl_init($url); |
03 | curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); |
04 | curl_setopt($ch,CURLOPT_HEADER, 0); |
05 | // EDIT your domain to the next line: |
06 | curl_setopt($ch,CURLOPT_USERAGENT,""); |
07 | curl_setopt($ch,CURLOPT_TIMEOUT,10); |
08 | $data = curl_exec($ch); |
09 | if (curl_errno($ch) !== 0 || curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { |
- Thêm domain name của bạn vào giữa ” ” trong dòng :
curl_setopt($ch,CURLOPT_USERAGENT,"");
- Ví dụ :
curl_setopt($ch,CURLOPT_USERAGENT,"izdesignz.com");
- Các bạn lưu ý là không có http:// nhé, chỉ domain name thôi.
Function: myfeeds_count
01 | // Get Feedburner RSS Subscriber count as plain text |
02 | add_option('myfeeds_count','0','','yes'); |
03 | add_option('myfeeds_api_timer',mktime() - 10000,'','yes'); |
04 | function myfeeds_count() { |
05 | $rsscount = get_option('myfeeds_count'); |
06 | if ( get_option('myfeeds_api_timer') < (mktime() - 3600) ) { |
07 | // EDIT your Feedburner feed name here: |
11 | $xml = new SimpleXmlElement($subscribers, LIBXML_NOCDATA); |
13 | $rsscount = (string) $xml->feed->entry['circulation']; |
14 | update_option('myfeeds_count', $rsscount); |
16 | } catch (Exception $e) { } |
17 | update_option('myfeeds_api_timer', mktime()); |
19 | //Echo the count if we got it |
20 | if($rsscount == 'N/A' || $rsscount == '0') { echo 'many other'; } |
21 | else { echo $rsscount; } |
- Thay
$fb_id = ""; ở dòng 8 bằng tên của feed mà bạn muốn show trong Feedburner.
- Ví dụ link của feed izdesignz là : http://feeds.feedburner.com/izdesignz thì chỉ cần lấy izdesignz điền vào sẽ được như sau :
$fb_id = "izdesignz";
Function: mytwitter_followers
01 | // Get Twitter Follower count as plain text |
02 | add_option('mytwitter_followers','0','','yes'); |
03 | add_option('mytwitter_api_timer',mktime() - 10000,'','yes'); |
04 | function mytwitter_followers() { |
05 | $twittercount = get_option('mytwitter_followers'); |
06 | if ( get_option('mytwitter_api_timer') < (mktime() - 3600) ) { |
07 | // EDIT your Twitter user name here: |
11 | $xml = new SimpleXmlElement($followers, LIBXML_NOCDATA); |
13 | $twittercount = (string) $xml->followers_count; |
14 | update_option('mytwitter_followers', $twittercount); |
16 | } catch (Exception $e) { } |
17 | update_option('mytwitter_api_timer', mktime()); |
19 | if ( $twittercount != '0' ) { echo $twittercount; } |
20 | else { echo "growing number of"; } |
- Thay
$twitter_id = ""; ở dòng 8 bằng username Twitter của bạn.
- Ví dụ của izdesignz là http://twitter.com/izdesignz thì sẽ phải thay đổi như sau
$twitter_id = "izdesignz";
Bước 2 : Hiển thị số Feedburner Subscribers và Twitter Followers
Nếu bạn muốn hiển thị số subscribers thì bạn gọi hàm sau :
1 | <?php if (function_exists('myfeeds_count')) myfeeds_count(); ?> |
Còn nếu muốn hiển thị số twitter follower thì bạn gọi hàm sau :
1 | <?php if (function_exists('mytwitter_followers')) mytwitter_followers(); ?> |
0 nhận xét:
Đăng nhận xét