automatic_updates_complete
云策文档标注
概述
automatic_updates_complete 是一个 WordPress 动作钩子,在所有自动更新完成后触发,主要用于在更新后执行自定义操作。它传递一个包含更新结果的数组参数,方便开发者处理更新后的逻辑。
关键要点
- 钩子名称:automatic_updates_complete,在 WordPress 3.8.0 版本引入
- 触发时机:所有自动更新(如插件、主题、核心)运行完毕后
- 参数:$update_results(数组),包含所有尝试更新的结果信息
- 用途:允许开发者在自动更新完成后添加自定义代码,例如处理特定插件的更新后操作
代码示例
add_action( 'automatic_updates_complete', 'wpdocs_auto_update_complete' );
function wpdocs_auto_update_complete( $results ) {
foreach ( $results['plugin'] as $plugin ) {
if ( ! empty( $plugin->item->slug ) && '{plugin_slug}' === $plugin->item->slug ) {
// 执行自定义操作
}
}
}注意事项
- 参数 $update_results 是一个数组,具体结构需参考文档或调试以了解包含的数据,例如插件更新结果位于 'plugin' 键下
- 使用此钩子时,应确保代码高效,避免影响更新后的系统性能
原文内容
Fires after all automatic updates have run.
Parameters
$update_resultsarray-
The results of all attempted updates.
Source
do_action( 'automatic_updates_complete', $this->update_results );
Changelog
| Version | Description |
|---|---|
| 3.8.0 | Introduced. |
Skip to note 2 content
MattDotNet
This hook is pretty useful in case you need to perform some stuff after an automatic update gets completed by WordPress. I personally spent a lot of time figuring out how to deal with this feature, especially because I didn’t know what kind of data the
$update_resultsparameter contained.So, here’s a working example of how you should use this hook.
/** * Fires after all automatic updates have run. * Completes the update scheduled in background. * * @param array $results The results of all attempted updates. * * @since 3.8.0 */ add_action( 'automatic_updates_complete', 'wpdocs_auto_update_complete' ); function wpdocs_auto_update_complete( $results ) { // the list of plugins is contained within the "plugins" attribute foreach ( $results['plugin'] as $plugin ) { // make sure the plugin slug matches the one assigned to your own plugin if ( ! empty( $plugin->item->slug ) && '{plugin_slug}' === $plugin->item->slug ) { /** * @todo Plugin found! Do stuff here... */ } } }