
Cron Jobs in WordPress to Send Email after every Hour
- November 23, 2015
- Leave a comment
Cron jobs are used to schedule the execution of a specific task at a specific time or after a defined interval. These jobs are usually executed periodically. Cron is a UNIX command. WordPress uses wp-cron to schedule a cron job. In this article, you will learn how to set up a cron job to send out an email automatically to the provided email address after every hour.
Use the following code to schedule an automatic email sender after every hour:
1 2 3 4 5 6 7 8 |
// send automatic scheduled email if (!wp_next_scheduled('pt_my_task_hook')) { wp_schedule_event(time(), 'hourly', 'pt_my_task_hook'); } add_action('pt_my_task_hook', 'pt_my_task_function'); function pt_my_task_function() { wp_mail('you@yoursite.com', 'Subject Goes Here', 'Email Content Goes Here'); } |
You can alternate recipient email address, subject and content in the wp_mail function.
Where does this code go?