WordPress利用“伪cron”机制实现定时任务。如果想把某些功能加到WordPress的cron里,可以使用下面的方法。首先打开functions.php文件,粘贴入以下代码:

WordPress运用“伪cron”机制完成守时使命。假如想把某些功用加到WordPress的cron里,能够运用下面的办法。

在WordPress里履行守时使命(wordpress定时任务)  定时任务 开发设计 新闻资讯 第1张

首要翻开functions.php文件,粘贴入以下代码:

if (!wp_next_scheduled('my_task_hook')) {
wp_schedule_event( time(), 'hourly', 'my_task_hook' );
}

add_action( 'my_task_hook', 'my_task_function' );

function my_task_function() {
wp_mail('you@yoursite.com', 'Automatic email', 'Hello, this is an automatically scheduled email from WordPress.');
}

留意,在榜首行里,咱们创建了一个使命,并承认使命名没有重复。然后经过add_action函数把使命加进WordPress的守时使命中。而在使命函数的声明中,咱们能够看到这个使命是用来发送邮件的。

转载请说明出处
知优网 » 在WordPress里履行守时使命(wordpress定时任务)

发表评论

您需要后才能发表评论