in_plugin_update_message-{$file}
云策文档标注
概述
in_plugin_update_message-{$file} 是一个 WordPress 动态 Hook,用于在插件列表表格的每一行更新消息容器末尾触发,允许开发者自定义插件更新时的显示信息。
关键要点
- Hook 名称是动态的,{$file} 指插件主文件相对于 plugins 目录的路径。
- 传递两个参数:$plugin_data(插件元数据数组)和 $response(可用插件更新的元数据对象)。
- 常用于在插件更新行中添加自定义通知或警告信息。
代码示例
function wpdocs_plugin_update_message( $plugin_data, $new_data ) {
if ( isset( $plugin_data['update'] ) && $plugin_data['update'] && isset( $new_data->upgrade_notice ) ) {
printf(
'%s: %s',
$new_data -> new_version,
wpautop( $new_data -> upgrade_notice )
);
}
}
add_action( 'in_plugin_update_message-my-plugin-name/my-plugin-name.php', 'wpdocs_plugin_update_message', 10, 2 );注意事项
- $response 参数是一个对象,而非数组,需注意其属性访问方式。
- upgrade_notice 属性可能在某些情况下不可用,代码应做兼容性处理。
- 此 Hook 自 WordPress 2.8.0 版本引入。
原文内容
Fires at the end of the update message container in each row of the plugins list table.
Description
The dynamic portion of the hook name, $file, refers to the path of the plugin’s primary file relative to the plugins directory.
Parameters
$plugin_dataarray-
An array of plugin metadata. See get_plugin_data() and the ‘plugin_row_meta’ filter for the list of possible values.
$responseobject-
An object of metadata about the available plugin update.
idstringPlugin ID, e.g.w.org/plugins/[plugin-name].slugstringPlugin slug.pluginstringPlugin basename.new_versionstringNew plugin version.urlstringPlugin URL.packagestringPlugin update package URL.iconsstring[]An array of plugin icon URLs.bannersstring[]An array of plugin banner URLs.banners_rtlstring[]An array of plugin RTL banner URLs.requiresstringThe version of WordPress which the plugin requires.testedstringThe version of WordPress the plugin is tested against.requires_phpstringThe version of PHP which the plugin requires.
Source
do_action( "in_plugin_update_message-{$file}", $plugin_data, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Geoffrey
$responseseems to be an object, not an array here.I used this hook to be able to display an upgrade notice message just after the new version message, like so:
function wpdocs_plugin_update_message( $plugin_data, $new_data ) { if ( isset( $plugin_data['update'] ) && $plugin_data['update'] && isset( $new_data->upgrade_notice ) ) { printf( '<div class="update-message"><p><strong>%s</strong>: %s</p></div>', $new_data -> new_version, wpautop( $new_data -> upgrade_notice ) ); } } add_action( 'in_plugin_update_message-my-plugin-name/my-plugin-name.php', 'wpdocs_plugin_update_message', 10, 2 );But it seems like
upgrade_noticeisn’t available anymore. This code is still a good way to warn user for important upgrades. 🙂