php - integrating twitter feed into a shortcode for wordpress -
i'm trying create simple shortcode wordpress has following variables: - username - no_of_tweets
so user write [twitter_feed username="username" no_of_tweets="5"]
and displays list of 5 of latest tweets. have been using plugin called twitter feed developers - oauth stuff , idea write code output front end html.
i have working except 1 annoying glitch - can't no_of tweets work.
this code works, doesn't allow users specify no_of_tweets variable:
extract( shortcode_atts( array( 'username' => 'skizzar_sites', 'no_of_tweets' => '3', ), $atts ) ); // code $tweets = gettweets(3, $username); ...
if tochange code following (i.e. change "3" in $tweets variable, code stops working:
extract( shortcode_atts( array( 'username' => 'skizzar_sites', 'no_of_tweets' => '5', ), $atts ) ); // code $tweets = gettweets($no_of_tweets, $username);
is there reason why might not working correctly?
see full code below:
<?php // add shortcode function skizzar_twitter_feed( $atts ) { // attributes extract( shortcode_atts( array( 'username' => 'skizzar_sites', 'no_of_tweets' => '5', ), $atts ) ); // code $tweets = gettweets($no_of_tweets, $username);//change number 20 number of tweets if(is_array($tweets)){ // use intents echo '<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>'; foreach($tweets $tweet){ if($tweet['text']){ $the_tweet = $tweet['text']; if(is_array($tweet['entities']['user_mentions'])){ foreach($tweet['entities']['user_mentions'] $key => $user_mention){ $the_tweet = preg_replace( '/@'.$user_mention['screen_name'].'/i', '<a href="http://www.twitter.com/'.$user_mention['screen_name'].'" target="_blank">@'.$user_mention['screen_name'].'</a>', $the_tweet); } } if(is_array($tweet['entities']['hashtags'])){ foreach($tweet['entities']['hashtags'] $key => $hashtag){ $the_tweet = preg_replace( '/#'.$hashtag['text'].'/i', '<a href="https://twitter.com/search?q=%23'.$hashtag['text'].'&src=hash" target="_blank">#'.$hashtag['text'].'</a>', $the_tweet); } } if(is_array($tweet['entities']['urls'])){ foreach($tweet['entities']['urls'] $key => $link){ $the_tweet = preg_replace( '`'.$link['url'].'`', '<a href="'.$link['url'].'" target="_blank">'.$link['url'].'</a>', $the_tweet); } } echo $the_tweet; echo ' <ul class="twitter_intents"> <li><a class="reply" href="https://twitter.com/intent/tweet?in_reply_to='.$tweet['id_str'].'"><i class="fa fa-reply"></i></a></li> <li><a class="retweet" href="https://twitter.com/intent/retweet?tweet_id='.$tweet['id_str'].'"><i class="fa fa-retweet"></i></a></li> <li><a class="favorite" href="https://twitter.com/intent/favorite?tweet_id='.$tweet['id_str'].'"><i class="fa fa-star"></i></a></li> </ul>'; echo ' <p class="timestamp"> <a href="https://twitter.com/'.$username.'/status/'.$tweet['id_str'].'" target="_blank"> '.date('h:i m d',strtotime($tweet['created_at']. '- 8 hours')).' </a> </p>';// -8 gmt pacific standard time } else { echo ' <br /><br /> <a href="http://twitter.com/'.$username.'" target="_blank">click here read '.$username.'\'s twitter feed</a>'; } } } } add_shortcode( 'twitter_feed', 'skizzar_twitter_feed' );
here gettweets function code:
/* implement gettweets */ function gettweets($username = false, $count = 20, $options = false) { $config['key'] = get_option('tdf_consumer_key'); $config['secret'] = get_option('tdf_consumer_secret'); $config['token'] = get_option('tdf_access_token'); $config['token_secret'] = get_option('tdf_access_token_secret'); $config['screenname'] = get_option('tdf_user_timeline'); $config['cache_expire'] = intval(get_option('tdf_cache_expire')); if ($config['cache_expire'] < 1) $config['cache_expire'] = 3600; $config['directory'] = plugin_dir_path(__file__); $obj = new stormtwitter($config); $res = $obj->gettweets($username, $count, $options); update_option('tdf_last_error',$obj->st_last_error); return $res; }
Comments
Post a Comment