trying to get Twitter feed working in Wordpress theme

Hi I’m new to PHP and am in a ‘pickle’ as I need to get this Twitter widget working and the theme developer hasn’t responded to my emails. My client is getting aggravated and I need it fixed.

I hope one of you kind people could help me.

After I have set up the tokens keys etc to the Twitter widget…it will operate temporarily. However, after a while (last time was possibly an hour) an error will appear:

Warning: Invalid argument supplied for foreach() in /home/…/framework/inc/widgets/twitter/widget.php on line 157

If you any of you brave souls attempt to fix this for me THANK YOU SO MUCH IN ADVANCE!

The code for this as follows (and line 157 is blank):

<?php /* Plugin Name: Recent Tweets Widget Plugin URI: http://wordpress.org/extend/plugins/recent-tweets-widget/ Description: Recent Tweets Widget plugin for Twitter API v1.1 with Cache. It uses the new Twitter API v1.1 and stores tweets in the cache. It means that it will read status messages from your database and it doesn't query Twitter.com for every page load so you won't be rate limited. You can set how often you want to update the cache. Version: 1.0 Author: Theme Prince Author URI: http://themeprince.com */ // widget function class tp_widget_recent_tweets extends WP_Widget { public function __construct() { parent::__construct( 'tp_widget_recent_tweets', // Base ID 'Twitter Widget', // Name array( 'description' => __( 'Display recent tweets', 'moutheme' ), ) // Args ); } //widget output public function widget($args, $instance) { extract($args); if(!empty($instance['title'])){ $title = apply_filters( 'widget_title', $instance['title'] ); } echo $before_widget; if ( ! empty( $title ) ){ echo $before_title . $title . $after_title; } //check settings and die if not set if(empty($instance['consumerkey']) || empty($instance['consumersecret']) || empty($instance['accesstoken']) || empty($instance['accesstokensecret']) || empty($instance['cachetime']) || empty($instance['username'])){ echo 'Please fill all widget settings!' . $after_widget; return; } //check if cache needs update $tp_twitter_plugin_last_cache_time = get_option('tp_twitter_plugin_last_cache_time'); $diff = time() - $tp_twitter_plugin_last_cache_time; $crt = $instance['cachetime'] * 3600; // yes, it needs update if($diff >= $crt || empty($tp_twitter_plugin_last_cache_time)){ if(!require_once('twitteroauth.php')){ echo 'Couldn\'t find twitteroauth.php!' . $after_widget; return; } function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); return $connection; } $connection = getConnectionWithAccessToken($instance['consumerkey'], $instance['consumersecret'], $instance['accesstoken'], $instance['accesstokensecret']); $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$instance['username']."&count=10") or die('Couldn\'t retrieve tweets! Wrong username?'); if(!empty($tweets->errors)){ if($tweets->errors[0]->message == 'Invalid or expired token'){ echo ''.$tweets->errors[0]->message.'!
You\'ll need to regenerate it here!' . $after_widget; }else{ echo ''.$tweets->errors[0]->message.'' . $after_widget; } return; } for($i = 0;$i <= count($tweets); $i++){ if(!empty($tweets[$i])){ $tweets_array[$i]['created_at'] = $tweets[$i]->created_at; $tweets_array[$i]['text'] = $tweets[$i]->text; $tweets_array[$i]['status_id'] = $tweets[$i]->id_str; } } } //convert links to clickable format function convert_links($status,$targetBlank=true,$linkMaxLen=250){ // the target $target=$targetBlank ? " target=\"_blank\" " : ""; // convert link to url $status = preg_replace("/((http:\/\/|https:\/\/)[^ ) ]+)/e", "''. ((strlen('$1')>=$linkMaxLen ? substr('$1',0,$linkMaxLen).'...':'$1')).''", $status); // convert @ to follow $status = preg_replace("/(@([_a-z0-9\-]+))/i","$1",$status); // convert # to search $status = preg_replace("/(#([_a-z0-9\-]+))/i","$1",$status); // return the status return $status; } //convert dates to readable format function relative_time($a) { //get current timestampt $b = strtotime("now"); //get timestamp when tweet created $c = strtotime($a); //get difference $d = $b - $c; //calculate different time values $minute = 60; $hour = $minute * 60; $day = $hour * 24; $week = $day * 7; if(is_numeric($d) && $d > 0) { //if less then 3 seconds if($d < 3) return "right now"; //if less then minute if($d < $minute) return floor($d) . " seconds ago"; //if less then 2 minutes if($d < $minute * 2) return "about 1 minute ago"; //if less then hour if($d < $hour) return floor($d / $minute) . " minutes ago"; //if less then 2 hours if($d < $hour * 2) return "about 1 hour ago"; //if less then day if($d < $day) return floor($d / $hour) . " hours ago"; //if more then day, but less then 2 days if($d > $day && $d < $day * 2) return "yesterday"; //if less then year if($d < $day * 365) return floor($d / $day) . " days ago"; //else return more than a year return "over a year ago"; } } $tp_twitter_plugin_tweets = maybe_unserialize(get_option('tp_twitter_plugin_tweets')); if(!empty($tp_twitter_plugin_tweets)){ print '
    '; $fctr = '1'; foreach($tp_twitter_plugin_tweets as $tweet){ print '
  • '.convert_links($tweet['text']).'
    '.relative_time($tweet['created_at']).'
  • '; if($fctr == $instance['tweetstoshow']){ break; } $fctr++; } print '
'; } echo $after_widget; } //save widget settings public function update($new_instance, $old_instance) { $instance = array(); $instance['title'] = strip_tags( $new_instance['title'] ); $instance['consumerkey'] = strip_tags( $new_instance['consumerkey'] ); $instance['consumersecret'] = strip_tags( $new_instance['consumersecret'] ); $instance['accesstoken'] = strip_tags( $new_instance['accesstoken'] ); $instance['accesstokensecret'] = strip_tags( $new_instance['accesstokensecret'] ); $instance['cachetime'] = strip_tags( $new_instance['cachetime'] ); $instance['username'] = strip_tags( $new_instance['username'] ); $instance['tweetstoshow'] = strip_tags( $new_instance['tweetstoshow'] ); if($old_instance['username'] != $new_instance['username']){ delete_option('tp_twitter_plugin_last_cache_time'); } return $instance; } //widget settings form public function form($instance) { $defaults = array( 'title' => '', 'consumerkey' => '', 'consumersecret' => '', 'accesstoken' => '', 'accesstokensecret' => '', 'cachetime' => '', 'username' => '', 'tweetstoshow' => '' ); $instance = wp_parse_args( (array) $instance, $defaults ); echo '

Title:

Consumer Key:

Consumer Secret:

Access Token:

Access Token Secret:

Cache Tweets in every: hours

Twitter Username:

Tweets to display: '; $i = 1; for(i; $i <= 10; $i++){ echo ''.$i.''; } echo '

'; } } // register widget function register_tp_twitter_widget(){ register_widget('tp_widget_recent_tweets'); } add_action('widgets_init', 'register_tp_twitter_widget'); //add_action('init', 'register_tp_twitter_widget', 1) ?>

Let’s trace it to the source and see what we can figure out.

[php]Warning: Invalid argument supplied for foreach() in /home/…/framework/inc/widgets/twitter/widget.php on line 157[/php]

Which is this line:
[php]foreach($tp_twitter_plugin_tweets as $tweet){ [/php]

Let’s check out where $tp_twitter_plugin_tweets is coming from…
[php]$tp_twitter_plugin_tweets = maybe_unserialize(get_option(‘tp_twitter_plugin_tweets’));[/php]

And this is where it ends because no where further up in the code is there another instance of “maybe_unserialize” or “tp_twitter_plugin_tweets” - You need to find what those belong to and you should have a better chance of fixing the issue.

Also, I saw this…it looks outdated, but maybe you can try a couple of their solutions: http://wordpress.org/support/topic/warning-invalid-argument-5

Sponsor our Newsletter | Privacy Policy | Terms of Service