1

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

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.

3
  • Just FYI: that "snippet" is using update_option(), which means that you must run it as a user with manage_options capability - or, in other words, and Administrator-role user account. Jul 19, 2011 at 15:51
  • @Chip say what? update_option() is very generic and will work just fine even without login. manage_options by definition controls access to option panels in admin area (and is often used as synonym for administrator role), it is unrelated to Options API.
    – Rarst
    Jul 21, 2011 at 12:08
  • Yeah, I clarified in my answer below. :) The real problem isn't likely update_option() but rather fopen() and incorrect code to begin with. Jul 21, 2011 at 13:58

4 Answers 4

2

Ouch, that snippet is ancient (I am kinda its author, more precisely adapted to twitter from other snippet). I would strongly consider using some newer solution.

I had later made newer and written from scratch version, try it http://www.rarst.net/script/twitter-counts-wordpress/

3
  • I know this is super old, but I'm looking for a Twitter follower count snippet again and found this old question of mine. NO snippet I tried worked. They all just didn't display anything at all. Yours is at least outputting numbers, though they're completely wrong (e.g. displaying 900-something when I'm close to 5000 followers). Is this still up-to-date? Thank you. :)
    – japanworm
    May 26, 2015 at 0:05
  • 1
    @japanworm twitter had killed off its public API since, the only way to get something working these days is with registration and authentication, personally I won't bother writing any snippets for that, it's not worth the effort...
    – Rarst
    May 26, 2015 at 6:52
  • I've seen newer code snippets with customer keys and whatnot. Tried all of those. None of them worked for me. Guess I'll just keep putting in the number manually then. Thank you. :)
    – japanworm
    May 26, 2015 at 8:21
2

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?
6
  • Thank you. I tried it, but I still get the "Call to undefined function" error. I put it into the function.php of my child theme. Should I put it into the one of my parent theme instead?
    – japanworm
    Jul 20, 2011 at 13:30
  • Can you post, rather than merely linking to, your actual error message? Jul 20, 2011 at 13:35
  • Sure, sorry: 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
    – japanworm
    Jul 21, 2011 at 4:52
  • Okay, see edited answer, with some further questions. Jul 21, 2011 at 11:53
  • I put it into a sidebar widget that can accept PHP code. Maybe that's why?! I want the Twitter count to appear in my sidebar, that's why.
    – japanworm
    Jul 21, 2011 at 16:28
1

If the file is in your theme folder, instead of using include, try get_template_part. get_template_part( 'twitter.php' );

3
  • Now I don't get an error message anymore, but just nothing at all. It's supposed to display a number, right? Thanks for trying.
    – japanworm
    Jul 19, 2011 at 13:49
  • 1
    It is supposed to display a number. If you're not getting anything, it seems that the code snippet you found isn't working. Jul 19, 2011 at 14:56
  • I tried various different code snippets for displaying the number of Twitter followers already and none of them seems to work for me. Must be something else. Thanks.
    – japanworm
    Jul 20, 2011 at 13:35
1

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' );
3
  • I put the twitter.php into my theme folder.
    – japanworm
    Jul 19, 2011 at 13:49
  • @japanworm - see my edit above.
    – Milo
    Jul 19, 2011 at 15:32
  • That doesn't work either. Now I get the following error: "failed to open stream: no suitable wrapper could be found".
    – japanworm
    Jul 20, 2011 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.