One of the most frequent questions I get regarding the Tweet Import plugin, is whether the update, or import, frequency can be changed or, in other terms, if the plugin can be made to import tweets faster than the default 15 minutes minimum allowed interval.

My reply is usually that technically speaking it is possible but there are some considerations to take care of first.

First of all, it is important to understand that Tweet Import uses the built in WordPress scheduling engine which runs scheduled tasks at the end of page loads. What does that mean? It means that if nobody is browsing your website, the scheduled events will not run and therefore tweets will not be imported. Having said that, you need to make sure that you have enough traffic to your website for the scheduled events to run at the set intervals.

In principle, adding more intervals is easy and it requires updating both the frequency options and the caching duration of the WordPress internal RSS processor, SimplePie.

Adding frequency options to the plugin is easy. As a matter of fact, Tweet Import already adds two options “Once Every 15 Minutes” and “Four Times a Day” to the default WordPress built in schedule intervals like that:

if (!has_filter ('cron_schedules', 'skinju_tweetimport_add_schedule_intervals')) {add_filter('cron_schedules', 'skinju_tweetimport_add_schedule_intervals');}
if (!function_exists('skinju_tweetimport_add_schedule_intervals')):
function skinju_tweetimport_add_schedule_intervals()
{
  return array( 'everyquarter' => array('interval' => 900, 'display' => 'Once Every 15 Minutes'),
                '4timesaday' => array('interval' => 21600, 'display' => 'Four Times a Day'));
}
endif; //skinju_tweetimport_add_schedule_intervals

If you would like to add more frequencies, like let’s say “Every 5 Minutes”, you can either edit the plugin itself, or better yet, to still be able to update the plugin automatically and not lose the changes you made, add the following to your theme “functions.php” file:

// *** Requires Tweet Import Version 1.3 or newer ***
if (!has_filter ('cron_schedules', 'more_schedule_intervals')) {add_filter('cron_schedules', 'more_schedule_intervals', 100);}
if (!function_exists('more_schedule_intervals')):
function more_schedule_intervals()
{
 return array( 'every5minutes' => array('interval' => 300, 'display' => 'Once Every 5 Minutes'),
               'everyquarter' => array('interval' => 900, 'display' => 'Once Every 15 Minutes'),
               '4timesaday' => array('interval' => 21600, 'display' => 'Four Times a Day'));
}
endif; //more_schedule_intervals

In brief, every interval requires the following to be defined. An internal name, “every5minutes”, an interval, the frequency in seconds, and a display name, “Once Every 5 Minutes” in this case.

As for the caching duration, as of version 1.3, Tweet Import allows changing the caching duration using an external function without changing the plugin code itself. Why is it being cached to start with? Right?

Well, when I first designed the plugin, I thought that since the plugin is supposed to import tweets once every 15 minutes minimum, and in order to prevent unnecessary calls to Twitter API in case something went wrong, the plugin can use a cached version of the Twitter response. By default, the plugin will cache the response just little bit less than 15 minutes (880 seconds to be exact). After all, the plugin should never process the cached version of the response anyway. Today, I still hope this design choice is right :)

In order to change the caching duration, and suppose you would like to change it to 295 seconds, you should add the following to your “functions.php” file.

// *** Requires Tweet Import Version 1.3 or newer ***
if (!has_filter ('tweetimport_cache_duration', 'other_cache_duration')) {add_filter('tweetimport_cache_duration', 'other_cache_duration', 100);}
if (!function_exists('other_cache_duration')):
function other_cache_duration($current_duration)
{
 return (295);
}
endif; //other_cache_duration

This second addition will change the caching duration to 295 seconds, which is a little bit less than the 5 minutes update interval of 300 seconds.