Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Chip Bennett Jul 19 '11 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 '11 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. –  Chip Bennett Jul 21 '11 at 13:58

4 Answers 4

up vote 2 down vote accepted

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/

share|improve this answer
    
Awesome, thanks!! –  japanworm Sep 9 '11 at 5:20

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?
share|improve this answer
    
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 '11 at 13:30
    
Can you post, rather than merely linking to, your actual error message? –  Chip Bennett Jul 20 '11 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/ex‌​ecphp.php(44) : eval()'d code on line 1 –  japanworm Jul 21 '11 at 4:52
    
Okay, see edited answer, with some further questions. –  Chip Bennett Jul 21 '11 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 '11 at 16:28

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

share|improve this answer
    
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 '11 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. –  chrisguitarguy Jul 19 '11 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 '11 at 13:35

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' );
share|improve this answer
    
I put the twitter.php into my theme folder. –  japanworm Jul 19 '11 at 13:49
    
@japanworm - see my edit above. –  Milo Jul 19 '11 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 '11 at 13:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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