WordPress Alternate Cron Job Using Google App Script

Posted on the 11 May 2020 by Pro Webguru @prowebguru

Have you ever wondered that you can use Google App Script to create an alternate cron for wordpress wp-cron.php ? Most of the websites have issues with running wp-cron.php properly.

Wanna know ? Then read on....

Lots of website developers or bloggers face issues with WordPress cron. If your website has less number of visitors then your wordpress cron may not run.

wp-cron.php is ran everytime someone visits a page of your website. So if you have scheduled a blog post to be published at particular time and if there is no visit at that time, then probably your scheduled post publishing time is gonna miss.

So alternate solution to WordPress cron not executing properly is, use external service. And call your wp-cron.php file.

You will find lots of services which offer you to create a cron job. Some of them are free, some are paid. And might have different limitations.

So, today I am going to show you how you can create your own Cron for wordpress, and execute your wp-cron.php file as per your choice. eg. Run wordpress wp-cron.php file every minute or every 5 minutes, hourly, daily etc.

We are going to use Google App Script for creating a cron schedule. It can used for executing recurring jobs eg. fetching data from, trigger a page url etc.

If you are creating your first Google App Script then visit my previous blog post about, how you can use Google App Script for fetching contents from RSS feed and schedule that as Cron job. I have explained complete process with screenshots for first timers.

You can always watch video below, it has all steps.

Create a free WordPress cron using Google App Script

Following is our Google App Script for executing wordpress wp-cron.php url as cron.

/*
Google Apps Script for Creating a free WordPress wp-cron.php CRON Job
@prowebguru 
http://www.prowebguru.com/ 
*/
function executeWordpressCronJob() {
  // Get Active Spreadsheet
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  // Access cell A1
  var dataRange = sheet.getRange("A1");
  // Get URL from cell A1
  var data = dataRange.getValues();
  var date = new Date();
  
  var row = data[0];
  var websiteAddress = row[0];  // First column, now contains URL
  var response;
  try {
    response = UrlFetchApp.fetch(websiteAddress);
    if(response.getResponseCode() == 200) {
      sheet.appendRow([date,'Successful']);
    } else {
      sheet.appendRow([date,'Issue']);
    }
  } catch(e) {
    if(e.toString().indexOf('returned code 404') > -1)
      sheet.appendRow([date,'404 Url not found']);
    else
      sheet.appendRow([date,e]);
  }
  SpreadsheetApp.flush();
}

Steps to create a WordPress Cron using Google Spreadsheet & create a Google App Script

  • For creating a cron job using Google App Script, you need to visit Google Drive.
  • Then create a new Google Spreadsheet.
  • Save sheet by giving a proper name.
  • In the first cell, mention complete url of wp-cron.php with domain name.
  • Then goto Tools menu and click on "Script Editor".
  • Save script by giving a proper name.
  • Copy above script in the code section of the script.
  • Save script.
  • Then click on Edit menu and click on "Current project's triggers".
  • A new page is opened, click on "Add Trigger".
  • In the section "Choose which function to run", by default our function "executeWordpressCronJob" will be selected as we have only one function.
  • "Choose which deployment should run" will be as it is, "Head".
  • In the "Select event source" section, select "Time-driven".
  • Next in the "Select type of time based trigger" section, select "Minutes timer".
  • Now in the "Select minute interval" section, select "Every minute".
  • Lastly the "Failure notification settings", you can select as per your choice. So this notification will send you an email, if your script fails for some reason.
  • Once this is done, click on "Save".

Its done. You have created your WordPress wp-cron.php cron job using Google Spreadsheet and Google App Script. Our code also has capability to keep a record of every execution. If you don't want to keep a track of every execution, then just remove or comment lines having "sheet.appendRow".

You can also extend this script to run cron job for multiple wordpress sites/urls. Or read data from list of urls and then process it.

Is it useful to you ? Did it solve your issues ?