Show Twitter followers count snippets don’t work

No matter what I try I can’t seem to get the code snippets to work that will display the number of followers of Twitter.
I tried various ones as the Plugin I’m using is not showing the Twitter number at all, so I need to look for alternatives.

When I tried this snippet, for example, I got the following error message:
http://pastebin.com/iif9g35a

Read More

Anybody any idea what’s going wrong here?
I think it’s strange that the “Suscribers to text” plugin doesn’t display the number of followres to begin with – otherwise I wouldn’t have to look for alternatives.

Related posts

Leave a Reply

4 comments

  1. Two issues:

    1. The code you’re using requires Administrator-role capabilities
    2. The code you’re using requires fopen() wrappers, which may be disabled by your host

    You should probably be using wp_remote_get() instead of file_get_contents(). I would also recommend caching the result, rather than storing it as a DB option.

    But, let’s start with the basics. I’m going to assume that the preg_match() part of the WPBeginner code works, and re-wrap it in a more-flexible function.

    Try putting this in functions.php (change $user = 'wpbeginner' to your own username):

    <?php
    function mytheme_get_twitter_follower_count( $user = 'wpbeginner' ) {
        $twitter_url = 'http://twitter.com/users/show.xml?screen_name=' . $user;
        $twitter_data = wp_remote_get( $twitter_url );
        $xml = $twitter_data['body'];
        $twitter_followers = '0';
        if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
            $twitter_followers = $match[1];
        }
        return $twitter_followers;
    }
    ?>
    

    Then, wherever you want to output the string, put this in your template file:

    <p>Twitter Followers: <?php echo mytheme_get_twitter_follower_count(); ?></p>
    

    (Note that you can get the follower count for any user, by calling mytheme_get_twitter_follower_count( 'username' );.)

    EDIT

    From your error message:

    Twitter Followers: Fatal error: Call to undefined function mytheme_get_twitter_follower_count() in /hermes/bosweb25a/b155/ipg.zoomingjapancom/wp-content/plugins/php-code-widget/execphp.php(44) : eval()’d code on line 1

    1. Where are you putting the mytheme_get_twitter_widget_follower_count() function call?
    2. Where are you defining the mytheme_get_twitter_widget_follower_count() function?
    3. Why are you using a PHP code-execution Plugin?
    4. Why does that Plugin have eval() code in it?
  2. The snippet likely works, you’ve just not implemented it correctly.

    Where did you save the code from the first code section in that example? where are you including the code from?

    basically, the error is that the path to the file is not correct, it can’t find the twitter.php file you’re trying to include.

    EDIT –

    if the twitter.php file is in the root of your theme directory, try including it like this:

    include( TEMPLATEPATH . '/twitter.php' );