admin管理员组文章数量:1122832
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.
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.
Share Improve this question asked Nov 23, 2016 at 19:37 GowriGowri 8614 gold badges19 silver badges37 bronze badges 2 |1 Answer
Reset to default 0I 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 :)
本文标签: wp cronSchedule reminder at exact time
版权声明:本文标题:wp cron - Schedule reminder at exact time 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302432a1931531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
time()
) and11:30 pm
to check if>=
2 hours (HOUR_IN_SECONDS * 2
) then send the reminder? – Ismail Commented Nov 23, 2016 at 19:45