18 comments

  1. Alex (Viper007Bond)

    The advantages of transients over options are that they auto-expire (no need for your expiry check code, that’s done by WordPress on transient get) and that if you have an object cache enabled (such as memcache), then transients are stored only in the cache and not the database (unlike options which are stored in both).

    Transients are designed for exactly this kind of thing. :)

    Also, in my defense for my code only being able to do followers, I was merely trying to improve upon Konstantin Kovshenin’s code to show the best way to code such a feature rather than actually trying to come up with a comprehensive function.

    Regardless, nice code. :)

  2. Konstantin Kovshenin

    Awesome Rarst, good thinking with the ‘not only followers count’ part! It is sometimes useful to retrieve updates count, following count and maybe the user pic ;)

    Nice job!

    ~ K

  3. Val Archer

    Hi Rarst – thanks million for keeping your code updated here. I know nothing about php…

    (1) Do I place this code in my functions.php file?

    //from:
    function rarst_twitter_user(
    //up to + including:
    return $cache[$username][$field];
    }

    (2) Do I place this code in my html?

    (3) How do I re-write the above so it’s this one line, which was this in your old code:

    echo ‘Why are ‘.$tw['count'].’ readers following val archer on twitter today ?’;

    (4) where do I enter my username? Your old code had this line:

    $xml=file_get_contents(‘http://twitter.com/users/show.xml?screen_name=valarcher’);

    but I can’t find something similar in this new code.

    thank you for your kind help – Val

  4. Val Archer

    sorry (2) didn’t display, I’ll rewrite it with written as \ and /

    \?php
    echo rarst_twitter_user(‘rarst’, ‘name’).’ has ‘.
    rarst_twitter_user(‘rarst’, ‘followers_count’).’ followers after ‘.
    rarst_twitter_user(‘rarst’, ‘statuses_count’).’ updates.’;
    ?/

  5. Val Archer

    Hi Rarst – thank you! works great. I don’t get that “warning – failed to open stream” message any more, that I used to get as a result of twitter API “restrictive rules” (got it even after my web host white-listed my server at twitter).

    I guess no more error messages is because your new code “should be safe to use for multiply values and multiply users at the same time, without worrying about exhausting API limit.”

    P.S. you use “multiply” but in English it’s multiple, like multiple values + multiple users :) You multiply 2 x 3 to get 6, and 6 is a multiple of 2 (I think)

  6. WPExplorer

    Wow Rarst this is a super sweet peace of code. thanks so much for the tutorial!

  7. Luke

    This is working great. I am trying to figure out how I would get the status -> text?

  8. karthimx

    It works fine for me, thanks for the great functions.

  9. Tilo

    Thanks for the script. I have a question about it though. A couple of months ago i made my own little script to get my twitter followers. it worked on localhost but testing it on my server it didn’t work anymore. my first guess was twitter not allowing a lot of requests from the same server as my website is hosted on a shared server. i never went back to find out why it really didn’t work. now i’m thinking about trying to get your script to work. do you know anything about any restrictions by twitter?

  10. opa

    Hi and thanks for this.
    I wonder how I can use the function in a text widget or something.

5 pingbacks

  1. [...] Source: Rarst [...]

  2. [...] Source: Rarst [...]

  3. [...] Code Source: Rast [...]

  4. [...] Source [...]

  5. [...] not one for reinventing the wheel unnecessarily, so for all the Twitter stats I used @rarst‘s Twitter Snippet. The snippet allows you to grab the twitter username, followers and status count. It’s also [...]

Leave a Reply

You must be logged in to post a comment.

Subscribe to followup comments via RSS