朱建中 發表於 2023-7-13 00:00:00

在WordPress中使用wp-cron插件来设置定时任务

<p>
PHP 本身是无法创建定时任务的,但是 WordPress 自带了一个伪定时任务(Cron) API,非常的方便好用,包括 WordPress 本身的定时发布文章都依赖于这个 API</p>
<p>
WP Cron 是什么? 是 WordPress 一套定时触发机制, 可以循环安排任务执行. 如: 定时发布新文章, 定期检测版本等功能都是通过这个来实现的.</p>
<p>
WP Cron 可以为我们实现什么? 我们可以循环更新和提交网站数据, 节日定期向读者发送贺卡或者表单 ...</p>
<p>
<br>
它的原理就是将创建的定时任务存储到数据库里,当有人访问的时候就去判断一下是否到时间需要执行这个定时任务,如果到时间则执行。</p>
<p>
因为这种原理,所以执行的时间可能会有一些偏差,但随着网站的浏览量攀升和网络爬虫的不断访问,会让定时任务执行的时间越来越准确。</p>
<p>
WP-Cron 效率不高, 但还是很方便好用的, 整理了一下相关函数的使用方法如下.</p>
<p>
<strong>函数</strong></p>
<p>
<strong>wp_get_schedule</strong></p>
<p>
通过勾子别名, 获取预定安排的勾子. 成功时返回循环周期类别 (hourly, twicedaily, daily, ...), 失败时返回 false.</p>
<div>
<div>
<div id="highlighter_92653">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php wp_get_schedule( </code><code>$hook</code><code>, </code><code>$args</code> <code>) ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$hook: 勾子别名<br>
$args: 勾子对应函数的参数数组 (可选)</p>
<p>
<strong>wp_get_schedules</strong></p>
<p>
WordPress 默认支持的循环周期类别有 hourly, twicedaily 和 daily. 通过该函数我们可以获取所有这些循环周期数组.</p>
<div>
<div>
<div id="highlighter_690767">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php wp_get_schedules() ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
在默认情况下, 由以上方法获得的数组对象如下.</p>
<div>
<div>
<div id="highlighter_399875">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
<div>
13</div>
<div>
14</div>
</td>
<td>
<div>
<div>
<code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'hourly'</code> <code>=&gt; </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'interval'</code> <code>=&gt; 3600,</code>
</div>
<div>
<code> </code><code>'display'</code> <code>=&gt; </code><code>'Once Hourly'</code>
</div>
<div>
<code> </code><code>),</code>
</div>
<div>
<code> </code><code>'twicedaily'</code> <code>=&gt; </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'interval'</code> <code>=&gt; 43200,</code>
</div>
<div>
<code> </code><code>'display'</code> <code>=&gt; </code><code>'Twice Daily'</code>
</div>
<div>
<code> </code><code>),</code>
</div>
<div>
<code> </code><code>'daily'</code> <code>=&gt; </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'interval'</code> <code>=&gt; 86400,</code>
</div>
<div>
<code> </code><code>'display'</code> <code>=&gt; </code><code>'Once Daily'</code>
</div>
<div>
<code> </code><code>)</code>
</div>
<div>
<code>)</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
我们可以向 cron_schedules 过滤器添加更多的类型. 添加例子如下:</p>
<div>
<div>
<div id="highlighter_305930">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
</td>
<td>
<div>
<div>
<code>add_filter(</code><code>'cron_schedules'</code><code>, </code><code>'cron_add_weekly'</code><code>); </code>
</div>
<div>
<code>function</code> <code>cron_add_weekly( </code><code>$schedules</code> <code>)</code>
</div>
<div>
<code>{</code>
</div>
<div>
<code> </code><code>// Adds once weekly to the existing schedules.</code>
</div>
<div>
<code> </code><code>$schedules</code><code>[</code><code>'weekly'</code><code>] = </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'interval'</code> <code>=&gt; 604800, </code><code>// 1周 = 60秒 * 60分钟 * 24小时 * 7天</code>
</div>
<div>
<code> </code><code>'display'</code> <code>=&gt; __(</code><code>'Once Weekly'</code><code>)</code>
</div>
<div>
<code> </code><code>);</code>
</div>
<div>
<code> </code><code>return</code> <code>$schedules</code><code>;</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>wp_next_scheduled</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
通过勾子别名, 获取预定安排的下一个运行时刻, 以整型返回. 常用于判断是否已经做了预定安排.</p>
<div>
<div>
<div id="highlighter_312099">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php </code><code>$timestamp</code> <code>= wp_next_scheduled( </code><code>$hook</code><code>, </code><code>$args</code> <code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$hook: 勾子别名<br>
$args: 勾子对应函数的参数数组 (可选)</p>
<p>
<strong>wp_schedule_event</strong></p>
<p>
按周期循环预定安排一个 WordPress 勾子, 在预定时间触发勾子对应的函数.</p>
<div>
<div>
<div id="highlighter_265838">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php wp_schedule_event(</code><code>$timestamp</code><code>, </code><code>$recurrence</code><code>, </code><code>$hook</code><code>, </code><code>$args</code><code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$timestamp: 时间 (整型)<br>
$recurrence: 循环周期类别 (hourly, twicedaily, daily, ...)<br>
$hook: 勾子别名<br>
$args: 勾子对应函数的参数数组 (可选)</p>
<p>
<strong>wp_reschedule_event</strong></p>
<p>
按周期循环重新预定安排一个 WordPress 勾子. 但我发现这个方法不能正常使用, Codex 写得很草, 如果哪位清楚知道怎么使用, 请告知一下.</p>
<p>
<strong>wp_unschedule_event</strong></p>
<p>
通过预定时间和勾子别名, 取消预定的安排.</p>
<p>
&lt;?php wp_unschedule_event($timestamp, $hook, $args ); ?&gt;<br>
$timestamp: 时间 (整型)<br>
$hook: 勾子别名<br>
$args: 勾子对应函数的参数数组 (可选)</p>
<p>
<strong>wp_clear_scheduled_hook</strong></p>
<p>
通过勾子别名, 移除预定安排的勾子.</p>
<p>
&lt;?php wp_clear_scheduled_hook( $hook ); ?&gt;<br>
$hook: 勾子别名</p>
<p>
<strong>wp_schedule_single_event</strong></p>
<p>
预定安排一个 WordPress 勾子, 在预定时间触发勾子对应的函数. 与 wp_schedule_event 不同的是该方法的只安排一次触发, 不存在循环预定.</p>
<div>
<div>
<div id="highlighter_97320">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>&lt;?php wp_schedule_single_event(</code><code>$timestamp</code><code>, </code><code>$hook</code><code>); ?&gt;</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
$timestamp: 时间 (整型)<br>
$args: 勾子对应函数的参数数组 (可选)</p>
<p>
从上面的函数可用的参数来看,我们就可以整理出以下几个常用的参数:</p>
<p>
<strong>参数</strong></p>
<p>
<strong>$timestamp</strong></p>
<p>
(整数)(必须)第一次执行此定时任务的时间,需要传一个时间戳,一般情况下都是当场执行,但不能用 time() 函数,而是用 WordPress 的时间函数 current_time()。</p>
<p>
默认值:None</p>
<p>
<strong>$recurrence</strong></p>
<p>
(字符串)(必须)执行频率。每隔多长时间执行一次。可以填写 hourly (每小时执行一次)、twicedaily (每天执行两次,也就是 12 小时执行一次)和 daily (24 小时执行一次)。</p>
<p>
默认值:None</p>
<p>
<strong>$hook</strong></p>
<p>
(字符串)(必须)执行的钩子。在执行定时任务的时候会调用这个钩子,往这个钩子挂在函数即可实现定时执行函数。<br>
默认值:None</p>
<p>
<strong>$args</strong></p>
<p>
(数组)(可选)传递的参数,会被传递到挂载到定时钩子的函数里的参数。</p>
<p>
默认值:None</p>
<p>
返回值</p>
<p>
(布尔 | null)如果添加成功则返回 null,不成功则返回 False</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_882652">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>if</code><code>( !wp_next_scheduled( </code><code>'test'</code> <code>) ) wp_schedule_event( current_time( </code><code>'timestamp'</code> <code>), </code><code>'twicedaily'</code><code>, </code><code>'test'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
首先使用 wp_next_scheduled() 函数判断是否已经创建,如果没创建则创建一个定时任务。</p>
<p>
把需要执行的代码挂载到 test 钩子上就行了。</p>
頁: [1]
查看完整版本: 在WordPress中使用wp-cron插件来设置定时任务