Wp_schedule_event functions.php

I am using this code in my functions.php which works fine. However, I was wondering if this is the best practice to do so to schedule an event, or if there is a better solution.

if (!wp_next_scheduled(‘my_task_hook’)) {
wp_schedule_event( time(), ‘daily’, ‘my_task_hook’ );
}

add_action( ‘my_task_hook’, ‘my_task_function’ );

function my_task_function() {
wp_mail(‘myemail@example.com’, ‘Test Email’, ‘Hello, this is a test automatically scheduled email from WordPress.’);
}

Read more here:: Wp_schedule_event functions.php

Leave a Reply

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