wordpress - 如何在预定的时间间隔内自动运行功能?
问题描述
我正在为一个 Wordpress 网站开发一个插件,我想在每个月的第一天生成一份报告并将其发送给用户/管理员。
在服务器中创建 Cron 作业将是一个完美的解决方案,但以编程方式创建 Cron 作业几乎没有障碍,因为该过程因服务器而异(我猜)。所以我正在考虑一个 WordPress 功能,而不是在执行这项工作的服务器中创建一个 Cron 作业。
如果你们对此有任何想法,请告诉我。
解决方案
如果间隔时间已经过去,它们只会在加载 WordPress 时运行
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
$schedules['every_three_minutes'] = array(
'interval' => 60,
'display' => __( 'Every 1 Minutes', 'textdomain' )
);
return $schedules;
}
// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}
// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/wordpressrootfolder/".time()."-myText.txt","wb");
fwrite($fp,$content);
fclose($fp);
}
推荐阅读
- kubernetes - Istio Ingress TLS 使用 ACM:上游连接错误或在标头之前断开/重置。重置原因:连接终止
- scala - SBT 拒绝继续下载
- asterisk - 星号,忽略 GET DATA 中的 DTMF 特殊字符
- amazon-web-services - 如何通过 Amazon 路由 53 映射带有端口号的 URL
- intellij-idea - 当我尝试配置 jdk 时,intellij 无法识别 jvm
- google-apps-script - Google 表格中动态生成的下拉菜单
- python - itertools.islice 和 read 函数的区别
- java - 在java中的正则表达式中使用“[”,“]”符号
- angular - Angular 应用程序在 PCF 上加载主页大约需要 30 秒
- android - 将 LineageOs 设备从 Cm-14.1 移植到 Lineage-16.0