WordPress Plugin Customization: Twitter Tools

Twitter Bird

Tweeting is nice because it’s terse; I can update my tweeps about what I’m doing, resources I’ve discovered and articles I’ve read between more lengthy and media-rich blog posts (and to publicize those posts).

The Twitter Tools plugin is a great way to integrate tweets into a WordPress blog. The most obvious benefit of this is that it keeps the content on the blog page fresh and allows bloggers to communicate bitlets of information that do not require their own post.

In addition to other worthwhile features, Twitter Tools allows a blog admin to display recent tweets in a sidebar widget, and to automatically publish tweets in a daily or weekly digest format. Twitter Tools caches tweets in a table of the WordPress database to reduce the number of calls to Twitter.

There were a few things about the plugin I didn’t particularly like out of the box. First, there was a link below the last tweet in the sidebar widget to take a visitor to my Twitter page. I prefer to have the widget title be that link. Second, the digest post title date format was ugly: “2009-06-29” instead of “June 29, 2009”. Third, the link at the end of each tweet in the digest post had a simple ‘#’ instead of the date and time of the tweet, which also serves as a link to the original tweet.

To fix the first problem all I had to do was modify the title variable of the aktt_widget function. aktt_profile_url($aktt->twitter_username) returns the URL of my Twitter updates. $aktt->twitter_username holds is the username set in the plugin options. It would have been simpler to hard code my Twitter URL here, but that would have rendered the modified plugin importable, which is a major programming faux pas. This modification does ignore the title set in the plugin options.

function aktt_widget($args) {
	global $aktt;
	extract($args);
	$options = get_option('aktt_widget');
	$title = '@<a href="'.aktt_profile_url($aktt->twitter_username).'">'.$aktt->twitter_username.'</a> on Twitter';
	if (empty($title)) {
	}
	echo $before_widget . $before_title . $title . $after_title;
	aktt_sidebar_tweets();
	echo $after_widget;
}

To hide the “More updates…” link at the bottom of the sidebar widget I simply commented out the code which adds it.

/*if (!empty($aktt->twitter_username)) {
  	$output .= '		<li class="aktt_more_updates"><a href="'.aktt_profile_url($aktt->twitter_username).'">More updates...</a></li>'."\n";
}*/

To fix the date format in the title of digest posts I simply found the array key which holds the post title and modified its value. Voila!

$post_data = array(
	'post_content' => $wpdb->escape($content),
	'post_title' => $wpdb->escape(sprintf($title, date('F j, Y'))),
	'post_date' => date('Y-m-d H:i:s', $end),
	'post_category' => array($this->blog_post_category),
	'post_status' => 'publish',
	'post_author' => $wpdb->escape($this->blog_post_author)
);

The third problem was the most troublesome, but still pretty simple. Why the plugin developer chose to use a hash mark (#) instead of the date and time of the tweet as a link to the actual tweet is curious to me. The hash is not only not informative, but used to indicate keywords in tweets, which can be confusing.

Twitter Weekly Updates for May 24, 2009

There are certainly positive aspects of hot weather. RT @ccmaine: Today’s weather is begging for a sundress and [high heels] :) #

What is the date and time of the tweet above? This modification adds valuable information while preserving the ability to click on a hyperlink to visit the original tweet.

Twitter Weekly Updates for June 28, 2009

Excellent ideas to get creative juices flowing! RT @problogger: 100 Creative Twitter Backgrounds Featuring Illustration – http://is.gd/1fZCW 27 Jun 2009 @ 08:27

This is all done in a switch statement of the aktt_tweet_display function.

switch ($time) {
	case 'relative':
		$time_display = aktt_relativeTime($tweet->tw_created_at, 3);
		break;
	case 'absolute':
		$time_display = aktt_absoluteTime($tweet->tw_created_at);
		break;
}

The value of the time_display variable was originally a boring '#'!

I borrowed code from the aktt_relativeTime function to write aktt_absoluteTime.

function aktt_absoluteTime ($date)
{
	$time = gmmktime(
		substr($date, 11, 2)
		, substr($date, 14, 2)
		, substr($date, 17, 2)
		, substr($date, 5, 2)
		, substr($date, 8, 2)
		, substr($date, 0, 4)
	);
 
	return date('j M Y @ H:i', $time);
}

This simply takes the tweet time as a parameter and returns a formatted date string. Not too bad, eh?

Next I’ll have to modify the plugin to make mentions clickable. Or I can wait for the update, which will wipe out my customizations. :)

19 Comment

  1. Can you change the formatting of Twitter Tools… I would love to frame the box that houses my tweets and have each tweet in it’s own framed box…..

  2. Hi, Sheryl. Yes! Anything can be changed, since you have access to the code. Your solution is to modify the CSS.

  3. Hey Brent, above you stated that your solution to changing the appearence is modifying the CSS. What i want to do is change the left padding of the actual tweets (class=”aktt_tweets”) but can’t seem to find this class anywhere in the twitter-tools/twitter-tools.php file. My test environment is here: http://kim.synapticate.com/. Could you shed any light on how to change this margin? Thanks in advance!

  4. Were the changes for the date and from “#” to username for the tweets listed as a blog and not the widget? Not a pro at coding – want to make sure. Thanks

  5. Kim,

    The $output variable is constructed beginning at line 761 (version 2.0) of twitter-tools.php (function aktt_sidebar_tweets()). You are correct that the class is aktt_tweets.

    To set the padding/margin in the CSS just create a selector in your theme’s CSS file (style.css).

    #sidebar div.aktt_tweets ul li { margin-left: 20px; }

    I hope this helps.

    Jen,

    Yes, in the digest posts. My modifications were overwritten when I recently upgraded the plugin. I’m doing a major retheme and will rewrite my Twitter Tools modifications when that is complete.

  6. Thanks for sharing. The code of the date part is exactly what I am looking for.

  7. Hi,
    Would you know if it’s possible to alter the title of the blog post that is automatically created from my tweets?

    Eg. instead of the post title being a shortened version of the tweet I could make the title for every post ‘via twitter’.

    Thanks,
    Phil.

  8. Phil, I’m not sure exactly what you mean by post title. Are you having Twitter Tools create a blog post whenever you tweet?

    1. Hi Brent,
      Yeah.
      I use my http://www.twitter.com/HUMMatTheBird account to to tweet.
      That tweet will then appear at http://hummatthebird.com/wednesdays/ as a blog post.
      Though I would like the title of the posts created to automatically say ‘via twitter’ instead of a snipped of the actual tweeted message.

      The site is in complete test phase though am just testing functionality.

      Thanks.

      1. function do_tweet_post($tweet) {
        		global $wpdb;
        		remove_action('publish_post', 'aktt_notify_twitter', 99);
        		$data = array(
        			'post_content' => $wpdb->escape(aktt_make_clickable($tweet->tw_text))
        			, 'post_title' => $wpdb->escape(trim_add_elipsis($tweet->tw_text, 30))
        			, 'post_date' => get_date_from_gmt(date('Y-m-d H:i:s', $tweet->tw_created_at))
        			, 'post_category' => array($this->blog_post_category)
        			, 'post_status' => 'publish'
        			, 'post_author' => $wpdb->escape($this->blog_post_author)
        		);

        Just change the 'post_title' value in the do_tweet_post function.

        1. Found that, thanks.
          Though not be too experienced with PHP I’ve added

          , ‘post_title’ => print ‘via Twitter’

          and the title now comes through as ‘1’

          Probably missing another simple thing…hah.

          1. The post_title line should be:

            , 'post-title' => 'via Twitter'

  9. Newbie here…
    Do I place those codes into custom.css?
    or…

    I haven’t found a Twitter PlugIn that I liked yet, but this one sounds great!

    And, is there a way to change the link colors?

    Thanks,
    Tammi

    1. Unfortunately, Tammi, no. You’d have to modify the plugin, which would mean future updates would break your customization. You can easily change the colors, though. Just add a class selector in your CSS file. The div has class “aktt_tweets.”

      Let me know if you require additional explanation. I’m happy to help.

  10. Hi Brent,

    Is there anyway I can have the tweets moved from my side panel and onto the page. I find the the widget on the side panel makes my site look chaotic.

    Thanks for the help!

    Bubbleboy

  11. I’m also trying Twitter Tools for WordPress and want to automatically post a tweet whenever I write a blog message with WordPress. But, I’d like to tweet the actual blogmessage (or the first part of it..) instead of the title. How do i do this? I’d like to see the first part of the blogpost on twitter with a link to the full blogpost.

  12. How do I make Twitter Tools post the content of my wordpress blog instead of the title?

  13. Hi Brent,

    Thanks for the great mods which I have used in my site.

    My timezone is GMT +10 and this is not reflected in the digest post. Is this because of your mod above or the Twitter Tools?

    also, do you know how to get a ‘:’ character to appear at the end of the title?

    Thanks

  14. Hi,

    I’d like to change the date format from 2012-04-13 to read Month / DD/YYY. It looks like your code is supposed to do that, but I’m not quite sure how or where to plug it into my twitter-tools.php file. I’m using v2.4 of the plugin if that is important.

    I think the section I need to tweak is this:
    function aktt_tweet_display($tweet, $time = ‘relative’) {
    global $aktt;
    $output = aktt_make_clickable(wp_specialchars($tweet->tw_text));
    if (!empty($tweet->tw_reply_username)) {
    $output .= ‘ tw_reply_username, $tweet->tw_reply_tweet).'” class=”aktt_tweet_reply”>’.sprintf(__(‘in reply to %s’, ‘twitter-tools’), $tweet->tw_reply_username).’‘;
    }
    switch ($time) {
    case ‘relative’:
    $time_display = aktt_relativeTime($tweet->tw_created_at, 3);
    break;
    case ‘absolute’:
    $time_display = ‘#’;
    break;
    }
    $output .= ‘ twitter_username, $tweet->tw_id).'” class=”aktt_tweet_time”>’.$time_display.’‘;
    $output = apply_filters(‘aktt_tweet_display’, $output, $tweet); // allows you to alter the tweet display output
    return $output;
    }

Leave a Reply