Alternative Method of Running a task in wordpress every X seconds

June 16, 2015

Sometimes I find it cumbersome to use the cpanel cron and I am not a massive fan of the wordpress cron, so I have devised a little bit of php code which allows me to run a script which is called through the wordpress footer.

So for example if you have a script which needs to be run every 30 minutes, hourly or daily you can use the code below.

NOTE: like the wordpress cron, you do actually need a visit to your site in order for it to execute, but if you have a relatively busy site, then it wont be a problem. I generally use this for data that needs to be updated once a day as I know that I will get at least one visit a day either from a person or a robot.

This is the process I use
– put an include file in the footer.php after the closing html tag
– store a “last updated date” inside of wordpress options
– some php to check the seconds between now and the last updated date
– depending on how many seconds are passed, run or exit the cron.


The code below could be made shorter and more elegant but for demonstration purposes it explains it well.

1. In your footer.php (in the theme folder) put something like this to your script (you can put this AFTER your closing html tag if you want)

2. Inside of the update-currency-rates.php file you can put the following code.

//specify the seconds between each time the cron should run.
//EG 86400 = 24 hours.
$interval_seconds = “86400”;

//specifies the time zone for your dates which is handy if you are in UK and your servers are on US time.
$time_zone = “Europe/London”;

//set the timezone for any dates within this script.

//get the last import date from wordpress options
$currency_last_import_date = get_option(“currency_last_import_date”);

//check if the date exists inside the options and if not define one just in case this is the first time its ever been run.
if(!$currency_last_import_date) {
update_option( “currency_last_import_date”, date(‘Y-m-d H:i:s’));

//set the $currency_last_import_date.
$currency_last_import_date = get_option(“currency_last_import_date”);

//set the last run time for the script which we got from wp options.
$last_run_time = $currency_last_import_date;

//set the time right now.
$time_right_now = date(‘Y-m-d H:i:s’)

//set the next run time.
$next_run_time = $last_run_time + $interval_seconds;

//count how many seconds between last run time and next run time and this will give us a value in seconds so we can know if we can run the script or not
$time1 = strtotime($time_right_now);
$time2 = strtotime($next_run_time);
$seconds = $time2 – $time1;

//parse the logic and then run or exit the script
if($seconds > 0) {
echo “Ooops! Dont run the update yet! Its too soon.”;
} else {
echo “Hooray! Script can be run!”;

//here you can update the options with the run date so next time its run you will have the last run date
update_option( “currency_last_import_date”, $time_right_now);

Thats really all there is to it. No need to muck about with cron in cpanel and no messy code inside your themes function file.