Vi har tidigare skrivit om en kod som låter dig visa antalet Twitter-följare som bidrog av Rarst. I den här artikeln kommer vi att dela en mer avancerad och mer elegant kod som låter dig visa twitterföljare räknar i WordPress. Återigen bidrog detta skript också av Rarst.
Funktioner
Denna funktion är inte limit till antal följare. Den kan hämta alla icke-nesterade värden som returneras av API-metoden Twitter users/show.
Den har två nivåer av cache:
- sökta värden lagras som array i databasen, med hjälp av WP alternativ, för $interval sekunder;
- API-responser lagras i minnet så att du kan söka efter valfritt antal fält utan att generera flera API-förfrågningar.
Detta bör vara säkert att använda för att multiplicera värden och multiplicera användare kl samma tid, utan att oroa sig för att uttömma API limit.
Tutorial
Öppna först ditt temas functions.php-fil och add to följande kod:
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]; }
Användning
När du har klistrat in funktionen kan du nu använda koden i vilken WordPress templates-fil du gillar. Klistra helt enkelt in följande kod:
echo rarst_twitter_user('wpbeginner', 'name').' has '. rarst_twitter_user('wpbeginner', 'followers_count').' followers after '. rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
Ovanstående kod kommer att visas ungefär så här:
WPBeginner har 5846 följare efter 1300 updates.
källa: Rarst
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!
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…
wpbeginner says
In the usage section, replace ”wpbeginner” with your twitter name.
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
Sandeep says
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 says
You would have to use CSS.. unfortunately we are not writing tutorials for those on this site.
Administratör
Ozh says
(You should display syntax hilited and cleanly formatted code, really)
Rarst says
Just noticed you lost + between time() and 60
Editorial Staff says
Thanks for the correction. We really appreciate you providing the awesome snippets for everyone to use. Your hard work is really appreciated.
Administratör
Angelo Beltran says
Nice! I like its flexibility. Cheers.
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.