auto_update_{$type}
概述
auto_update_{$type} 是一个 WordPress 过滤器钩子,用于控制核心、插件、主题或语言的自动更新行为。它允许开发者根据类型动态调整更新策略,并提供了参数和示例代码。
关键要点
- 钩子名称是动态的,$type 可以是 core、plugin、theme 或 translation,对应 auto_update_core、auto_update_plugin、auto_update_theme 和 auto_update_translation。
- 从 WordPress 3.7 开始,默认自动更新核心的小版本和开发版本以及翻译;WordPress 5.6 及以上新安装默认自动更新主版本,旧站点可手动启用。
- 参数包括 $update(布尔值或 null,用于检测是否有钩子连接)和 $item(更新对象)。
- 相关函数如 wp_is_auto_update_forced_for_item() 和 WP_Automatic_Updater::should_update() 用于检查更新状态。
- 变更记录:5.5.0 版本中 $update 参数接受 null 值,3.7.0 版本引入此钩子。
代码示例
// 禁用插件自动更新
add_filter( 'auto_update_plugin', '__return_false' );
// 禁用主题自动更新
add_filter( 'auto_update_theme', '__return_false' );
// 启用特定插件的自动更新
function wpdocs_enable_plugin_auto_updates( $value, $item ) {
if ( 'your-plugin-slug' === $item->slug ) {
return true;
}
return $value;
}
add_filter( 'auto_update_plugin', 'wpdocs_enable_plugin_auto_updates', 10, 2 );注意事项
- 此过滤器不会阻止 WordPress 通过 HTTP API 访问更新端点(如 api.wordpress.org 的检查 URL),若需完全停止更新检查,可移除相关动作钩子。
- 存在一个开放问题(Trac 票号 51126)关于文档化“更新对象”参数。
Filters whether to automatically update core, a plugin, a theme, or a language.
Description
The dynamic portion of the hook name, $type, refers to the type of update being checked.
Possible hook names include:
auto_update_coreauto_update_pluginauto_update_themeauto_update_translation
Since WordPress 3.7, minor and development versions of core, and translations have been auto-updated by default. New installs on WordPress 5.6 or higher will also auto-update major versions by default. Starting in 5.6, older sites can opt-in to major version auto-updates, and auto-updates for plugins and themes.
See the ‘allow_dev_auto_core_updates’, ‘allow_minor_auto_core_updates’, and ‘allow_major_auto_core_updates’ filters for a more straightforward way to adjust core updates.
Parameters
$updatebool|null-
Whether to update. The value of null is internally used to detect whether nothing has hooked into this filter.
$itemobject-
The update offer.
Source
$update = apply_filters( "auto_update_{$type}", $update, $item );
Skip to note 5 content
Jer Clarke
Note that the filters mentioned above will not stop WP from visiting the update endpoints using the HTTP API. The following URLs will be checked on a regular bases even with the filters above set to disable updates:
https://api.wordpress.org/core/version-check/...<br />
https://api.wordpress.org/plugins/update-check/...<br />
https://api.wordpress.org/themes/update-check/...<br />
If you want to completely stop update checking, e.g. for performance reasons while coding or during acceptance testing, removing the following filters will ensure that the update endpoints are not checked:
/**<br />
* ! These actually stop the HTTP API requests!<br />
* Copied from WP_Customize_Manager->construct()<br />
*/<br />
remove_action( 'admin_init', '_maybe_update_core' );<br />
remove_action( 'admin_init', '_maybe_update_plugins' );<br />
remove_action( 'admin_init', '_maybe_update_themes' );<br />
Skip to note 6 content
thejaydip
add the code snippet to disable automatic WordPress plugin updates.
add_filter( 'auto_update_plugin', '__return_false' );add the code snippet to disable automatic WordPress themes updates.
add_filter( 'auto_update_theme', '__return_false' );Skip to note 7 content
Dev Kabir
To enable automatic updates for a plugin programmatically, you can use the WordPress functions and filters available. Here’s an example of how you can achieve this:
/** * Enable automatic updates for a specific plugin. * * @param boolean $value Current auto-update status. * @param object $item Plugin update object. * @return boolean Updated auto-update status. */ function wpdocs_enable_plugin_auto_updates( $value, $item ) { if ( 'your-plugin-slug' === $item->slug ) { return true; // Enable auto-updates for the specified plugin } return $value; // Preserve auto-update status for other plugins } add_filter( 'auto_update_plugin', 'wpdocs_enable_plugin_auto_updates', 10, 2 );Skip to note 8 content
jmckinneyocp
There’s an open issue to document the “update offer” parameter: https://core.trac.wordpress.org/ticket/51126