0

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.

  • 1
    Wouldn't it be simple adding a cron than runs each 10 min and that checks the difference between now (time()) and 11:30 pm to check if >= 2 hours (HOUR_IN_SECONDS * 2) then send the reminder? – Samuel Elh Nov 23 '16 at 19:45
  • 1
    @SamuelElh is correct here, a cron is really the only way of doing this. – socki03 Nov 23 '16 at 20:58
0

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.