Two issues:
- The code you're using requires Administrator-role capabilities
- 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
- Where are you putting the
mytheme_get_twitter_widget_follower_count()
function call?
- Where are you defining the
mytheme_get_twitter_widget_follower_count()
function?
- Why are you using a PHP code-execution Plugin?
- Why does that Plugin have eval() code in it?
update_option()
, which means that you must run it as a user withmanage_options
capability - or, in other words, and Administrator-role user account.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.update_option()
but ratherfopen()
and incorrect code to begin with.