Categories
Computers Notweet Programming

WP Mystique Twitter Widget

The standard Twitter widget that comes with the Mystique theme has a couple of shortcomings, in my opinion. One, any shortened URL’s are displayed using Twitters t.co link instead of whatever shortener the user may actually be using. I’ve installed a YOURLS site for just this purpose, so I’d like to see my site displayed. Second, hashtags are not linked back to a Twitter search.

Below the fold are a few lines of PHP that will address these problems. A brief word to the wise, these mods only work with Twitter’s API V1. API V1.1 will require OAuth to perform this task.

All of the following modifications are done in the atom-widgets.php file.

First, the code needs to request entity information from the server. In the AtomWidgetTwitter class, go to the `displayTweets’ function and change the following line:

$response = wp_remote_retrieve_body(wp_remote_request("http://twitter.com/statuses/user_timeline/{$user}.json"));

to this:

$response = wp_remote_retrieve_body(wp_remote_request("https://api.twitter.com/1/statuses/user_timeline/{$user}.json?include_entities=1"));

Note that this is the line that will cause problems when the API changes over to V1.1.

The entity information contains a lot of extra, but useful, information. It lists all of the URL information for a tweet, as well as user mentions and hashtags. We’ll be using the URL and hashtag info.

Several lines down from the above code change is a foreach loop that goes through all the tweets and grabs the text associated with the each tweet. After these lines:

foreach($tweets as $tweet){
    $data['tweets'][$i]['text'] = esc_attr(strip_tags($tweet['text']));

add the following lines:

$data['tweets'][$i]['urls'] = $tweet['entities']['urls'];
$data['tweets'][$i]['hashtags'] = $tweet['entities']['hashtags'];

Finally, down several lines further embedded in a ul tag is a section of php code used to markup the tweet text for display in the widget. I’ll simply post the whole section here:

<?php
  $i = 0;
    foreach($data['tweets'] as $tweet){
        $i++;
        $pattern = '/\@(\w+)/';
        $replace = '<a rel="nofollow" href="http://twitter.com/$1">@$1</a>';
        $tweet['text'] = preg_replace($pattern, $replace, $tweet['text']);
        if (!empty($tweet['urls'])){
            foreach($tweet['urls'] as $url){
                $find = $url['url'];
                $replace = '<a rel="nofollow" href="'.$find.'">'.$url['expanded_url'].'</a>';
                $tweet['text'] = str_replace($find, $replace, $tweet['text']);
            }
        }
        if (!empty($tweet['hashtags'])){
            foreach($tweet['hashtags'] as $hashtag){
                $find = '#'.$hashtag['text'];
                $replace = '<a rel="nofollow" href="https://twitter.com/#!/search/%23'.$hashtag['text'].'">'.$find.'</a>';
                $tweet['text'] = str_replace($find, $replace, $tweet['text']);
            }
        }
        $tweet['text'] = convert_smilies($tweet['text']);

        $link = "http://twitter.com/{$user}/statuses/{$tweet['id']}";
        echo '<li class="entry">'.$tweet['text'].'<a class="date" href="'.$link.'"rel="nofollow">'.atom()->getTimeSince($tweet['created_at'], time()).'</a></li>';

        if ($i == $count) break;
    }
?>

The nested foreach loops do the bulk of the work, looping through all the URL and hashtag info and marking up the display URL’s with the proper URL as well as marking up the hashtags to link a search at Twitter. In all, about 20 or so extra lines of code added for an improved presentation and functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *