Schedule reminder at exact time

I have event website. I want send reminder about event exactly 2 hours before the event to subscribers.

So how can schedule wordpress to call the function exactly at time.

I know about wp_schedule and cron. But that doesn't helps me.

Don't recommend plugins. Because I am developing custom plugin. So what is the correct way to do that.

Example: if event start time is today 11:30 pm , I want to remind the event subscribers at exactly 9:30 pm.

Topic wp-cron scheduled-posts Wordpress

Category Web


I second SamuelElh comment and YES cron is the way to go. BUT, which cron? The Wordpress implementation of cron is a tricky implementation. How? Actually it is a cron fired by the visits your site gets, both (back-end and front-end), so, if your site is not getting a visitor every 10 minutes as suggested by Samuel, then your Wordpress will be missing cron schedule and may not send the e-mails on time.

(Reference: https://codex.wordpress.org/Function_Reference/wp_schedule_event)

With that said, and if your site is getting enough hits to keep the cron going on time then it is as easy as adding:

To your Plugin page, this is hourly but change as you like:

register_activation_hook(__FILE__, 'cron_activation_function'); //to add cron to the cron table when plugin gets activated.
add_action('the_name_tag_of_your_cron_in_cron_table', 'the_cron_function_to_run');
register_deactivation_hook(__FILE__, 'cron_deactivation_function'); //to remove cron from the cron table when plugin gets deactivated.

function cron_activation_function()
{
    if (!wp_next_scheduled('the_name_tag_of_your_cron_in_cron_table')) {
        wp_schedule_event(time(), 'hourly', 'the_name_tag_of_your_cron_in_cron_table');
    }
}

function cron_deactivation_function()
{
    wp_clear_scheduled_hook('the_name_tag_of_your_cron_in_cron_table');
}

function the_cron_function_to_run() 
{
    //do what ever you want then send the emails
}

I hope this answers your question. Happy coding :)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.