Skip to content

Custom WordPress Cron Job Setup

The CallCronjob class is designed to facilitate the creation and management of custom cron jobs in WordPress. By specifying a cron job name, interval, and function to be executed, you can automate tasks such as report generation, data syncing, or content updates at specified intervals. This class provides an easy way to set up these cron jobs programmatically.

You can find the full class implementation here.

  • cronName: A unique identifier for the cron job.
  • interval: The time interval in seconds for running the cron job.
  • functionName: The name of the function to be executed when the cron job is triggered.

The CallCronjob class can be easily implemented into any WordPress project.

Example 1: Schedule a Cron Job to Run Every Hour

Section titled “Example 1: Schedule a Cron Job to Run Every Hour”
<?php
new CallCronjob((object) array(
'cronName' => 'every_one_hour', // Name of the cron job
'interval' => 3600, // Interval in seconds (1 hour)
'functionName' => 'generate_hourly_report' // Function to be executed
));
?>

Example 2: Schedule a Cron Job to Run Every 12 Hours

Section titled “Example 2: Schedule a Cron Job to Run Every 12 Hours”
<?php
new CallCronjob((object) array(
'cronName' => 'every_twelve_hours', // Name of the cron job
'interval' => 43200, // Interval in seconds (12 hours)
'functionName' => 'send_summary_email' // Function to be executed
));
?>

Example 3 : send_summary_email with MailTo Class

Section titled “Example 3 : send_summary_email with MailTo Class”
<?php
// Define the send_summary_email function
function send_summary_email() {
// Create a new instance of the MailTo class to send an email
$mail = new MailTo((object) [
'email' => 'summary@terrhq.com', // Recipient's email address
'subject' => 'Daily Summary Report', // Email subject
'message' => '<h1>Daily Summary</h1><p>This is the summary of today\'s activity...</p>' // HTML message content
]);
// Any other logic related to the summary email can be placed here
}
// Schedule the cron job to call send_summary_email every 12 hours
new CallCronjob((object) array(
'cronName' => 'every_twelve_hours', // Name of the cron job
'interval' => 43200, // Interval in seconds (12 hours)
'functionName' => 'send_summary_email' // Function to be executed by the cron job
));
?>