Snippet: Set up a Cron Job in WordPress

You are normally using your server’s Crontab to schedule your cron jobs. If you do not have access to server panel or you do not understand its functionality correctly, you can use WordPress’s job scheduling to execute your crons. You can set up cron for hourly execution, daily execution and executing twice in a day. Here is how you can set up hourly and daily jobs.

Snippet

register_activation_hook(__FILE__, 'wpds_cron');
add_action('perform_hourly_tasks', 'perform_task_1');
add_action('perform_hourly_tasks', 'perform_task_2');
add_action('perform_daily_tasks', 'perform_task_3');

function wpds_cron() {
	wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'perform_hourly_tasks');
	wp_schedule_event( current_time( 'timestamp' ), 'daily', 'perform_daily_tasks');
}
function perform_task_1() {
	// This function is called every hour.
}
function perform_task_2() {
	// This function is called every hour.
}
function perform_task_3() {
	// This function is called once in a day.
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>