Nous avons vu dernièrement comment créer un Widget avec Wordpress. Voici maintenant comment afficher vos derniers Tweets dans un widget ! Cette méthode est en fait une adaptation de ce qui est utilisé dans le thème Mystique que j’utilisais avant sur ce blog. Pour faire cela il va me falloir quelques fonctions que j’ai mise dans le fichier lib/core.php.``` function objectToArray($object){ if(!is_object($object) && !is_array($object)) return $object; if(is_object($object)) $object = get_object_vars($object); return array_map(‘objectToArray’, $object); }
function timeSince($older_date, $newer_date = false){ $chunks = array( ‘year’ => 60 * 60 * 24 * 365, // 31,536,000 seconds ‘month’ => 60 * 60 * 24 * 30, // 2,592,000 seconds ‘week’ => 60 * 60 * 24 * 7, // 604,800 seconds ‘day’ => 60 * 60 * 24, // 86,400 seconds ‘hour’ => 60 * 60, // 3600 seconds ‘minute’ => 60, // 60 seconds ‘second’ => 1 // 1 second );
$newer_date = ($newer_date == false) ? (time()+(60*60*get_settings(“gmt_offset”))) : $newer_date; $since = $newer_date - $older_date;
foreach ($chunks as $key => $seconds) if (($count = floor($since / $seconds)) != 0) break;
$messages = array( ‘year’ => ' il y a %s an(s)', ‘month’ => ' il y a %s mois', ‘week’ => ' il y a %s semaines', ‘day’ => ' il y a %s jours', ‘hour’ => ' il y a %s heures', ‘minute’ => ' il y a %s minutes', ‘second’ => ' il y a %s secondes', ); return sprintf($messages[$key],$count); }
function display_twitter_data($id, $twituser, $twitcount, $twitdata = false){ $error = false; if(!$twitdata): if (false === ($twitdata = get_transient(‘mystique_twitter_’.$id))): require_once(ABSPATH.‘wp-includes/class-snoopy.php’); $snoopy = new Snoopy; $response = @$snoopy->fetch(“http://twitter.com/users/show/".$twituser.".json"); if ($response) $userdata = json_decode($snoopy->results, true); else $error = true; $response = @$snoopy->fetch(“http://twitter.com/statuses/user_timeline/”.$twituser.".json”); if ($response) $tweets = json_decode($snoopy->results, true); else $error = true; if(!$error): // for php < 5 (included JSON returns object) $userdata = objectToArray($userdata); $tweets = objectToArray($tweets);
$twitdata = array();
$twitdata\['user'\]\['profile\_image\_url'\] = $userdata\['profile\_image\_url'\];
$twitdata\['user'\]\['name'\] = $userdata\['name'\];
$twitdata\['user'\]\['screen\_name'\] = $userdata\['screen\_name'\];
$twitdata\['user'\]\['followers\_count'\] = $userdata\['followers\_count'\];
$i = 0;
foreach($tweets as $tweet):
$twitdata\['tweets'\]\[$i\]\['text'\] = $tweet\['text'\];
$twitdata\['tweets'\]\[$i\]\['created\_at'\] = $tweet\['created\_at'\];
$twitdata\['tweets'\]\[$i\]\['id'\] = $tweet\['id'\];
$i++;
endforeach;
endif;
endif; endif;
// only show if the twitter data from the database is newer than 6 hours if(!$error && is_array($twitdata[‘tweets’])): ?>
-
@$1';
$tweet\['text'\] = preg\_replace($pattern, $replace , $tweet\['text'\]);
$tweet\['text'\] = make\_clickable($tweet\['text'\]);
// remove +XXXX
$tweettime = substr\_replace($tweet\['created\_at'\],'',strpos($tweet\['created\_at'\],"+"),5);
$link = "http://twitter.com/".$twitdata\['user'\]\['screen\_name'\]."/statuses/".$tweet\['id'\];
echo '<li><span class="entry">' . $tweet\['text'\] .'<a class="date" href="'.$link.'" rel="nofollow">'.timeSince(abs(strtotime($tweettime . " GMT")),time()). '</a></span></li>';
$i++;
if ($i == $twitcount) break;
endforeach;
?>
Maintenant passons au widget à proprement parlé. On trouve son code dans **core/widgets.php**.
class Wanders_Last_Tweets extends WP_Widget {
function Wanders_Last_Tweets() {
//Constructor
$widget_ops = array(‘classname’ => ‘widget_wanders_last_tweets’, ‘description’ => ‘Afficher les derniers tweets’);
$this->WP_Widget(‘last_tweets’,'[WANDERS] Derniers Tweets !',$widget_ops);
}
function widget($args, $instance) {
// prints the widget
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance\['title'\]) ? ' ' : apply\_filters('widget\_title', $instance\['title'\]);
$login = empty($instance\['login'\]) ? ' ' : apply\_filters('widget\_login', $instance\['login'\]);
$number = empty($instance\['number'\]) ? ' ' : apply\_filters('widget\_number', $instance\['number'\]);
echo $before\_title . $title . $after\_title;
display\_twitter\_data(wp\_specialchars($\_GET\['widget\_id'\]),$login,$number);
echo $after\_widget;
}
function update($new\_instance, $old\_instance) {
//save the widget
$instance = $old\_instance;
$instance\['title'\] = strip\_tags($new\_instance\['title'\]);
$instance\['login'\] = strip\_tags($new\_instance\['login'\]);
$instance\['number'\] = strip\_tags($new\_instance\['number'\]);
return $instance;
}
function form($instance) {
//widgetform in backend
$instance = wp\_parse\_args( (array) $instance, array('title' => '','login' => '','number'=>''));
$title = strip\_tags($instance\['title'\]);
$number = strip\_tags($instance\['number'\]);
$login = strip\_tags($instance\['login'\]);
?>
<p>Afficher les derniers Tweets</p>
<p><label for="<?php echo $this->get\_field\_id('title'); ?>">Titre : <input class="widefat" id="<?php echo $this->get\_field\_id('title'); ?>" name="<?php echo $this->get\_field\_name('title'); ?>" type="text" value="<?php echo attribute\_escape($title); ?>" /></label></p>
<p><label for="<?php echo $this->get\_field\_id('login'); ?>">Login Twitter : <input class="widefat" id="<?php echo $this->get\_field\_id('login'); ?>" name="<?php echo $this->get\_field\_name('login'); ?>" type="text" value="<?php echo attribute\_escape($login); ?>" /></label></p>
<p><label for="<?php echo $this->get\_field\_id('number'); ?>">Nombre de Tweets : <input class="widefat" id="<?php echo $this->get\_field\_id('number'); ?>" name="<?php echo $this->get\_field\_name('number'); ?>" type="text" value="<?php echo attribute\_escape($number); ?>" /></label></p>
<?php
}
}
register_widget(‘Wanders_Last_Tweets’);
On n'oublie pas d'inclure ces deux fichiers dans **functions.php**.
include(‘lib/widgets.php’);
include (‘lib/core.php’);