2

我正在使用 wordpress 注册插件。我卡在用户到期。实际上,我想在会员注册一年后使会员到期。我想在到期前 1 个月通过电子邮件通知他们。我正在使用 add_action('init','my function name') 检查有多少用户将在一个月后过期并发送邮件。bt 每次用户访问该站点时都会运行此操作挂钩,这将使我的站点在每次用户访问时都加载得太慢。所以我想要一些可以让这段代码一天运行一次的东西。例如,当第一个用户访问该网站时,该代码将运行,并且在剩下的一整天内,无论有多少用户访问该网站,该代码都不会被调用。

4

1 回答 1

6

Wordpress 有一个内置的函数/API,可以完全按照您的意愿行事 - 每天/每小时/您指定的任何间隔都做某事。

http://codex.wordpress.org/Function_Reference/wp_schedule_event

从上面的页面无耻地采取

add_action( 'wp', 'prefix_setup_schedule' );
/**
 * On an early action hook, check if the hook is scheduled - if not, schedule it.
 */
function prefix_setup_schedule() {
    if ( ! wp_next_scheduled( 'prefix_daily_event' ) ) {
        wp_schedule_event( time(), 'daily', 'prefix_daily_event');
    }
}


add_action( 'prefix_daily_event', 'prefix_do_this_daily' );
/**
 * On the scheduled action hook, run a function.
 */
function prefix_do_this_daily() {
    // check every user and see if their account is expiring, if yes, send your email.
}

prefix_大概是为了保证不会和其他插件发生冲突,所以我建议你把这个改成独一无二的。

如果您想了解更多信息,请参阅http://wp.tutsplus.com/articles/insights-into-wp-cron-an-introduction-to-scheduling-tasks-in-wordpress/ 。

于 2013-09-29T11:08:10.543 回答