auto_plugin_theme_update_email
云策文档标注
概述
本文档介绍了 WordPress 中的 auto_plugin_theme_update_email 过滤器,用于在插件或主题自动后台更新后修改发送的邮件内容。开发者可以通过此过滤器自定义邮件的收件人、主题、正文和头部信息。
关键要点
- auto_plugin_theme_update_email 是一个过滤器,允许修改自动更新后发送的邮件参数。
- 过滤器接收 $email 数组参数,包含 to、subject、body、headers 等键,用于传递给 wp_mail()。
- 其他参数包括 $type(邮件类型,如 'success'、'fail'、'mixed')、$successful_updates(成功更新列表)和 $failed_updates(失败更新列表)。
- 过滤器在 WP_Automatic_Updater::send_plugin_theme_email() 方法中使用,自 WordPress 5.5.0 版本引入。
代码示例
add_filter( 'auto_plugin_theme_update_email', 'wpdocs_auto_update_email', 10, 4 );
function wpdocs_auto_update_email( $email, $type, $successful_updates, $failed_updates ) {
// 自定义主题行
$email['subject'] = 'Custom Subject: Updates on Your Site';
// 自定义正文内容
$email['body'] = 'Hello! Here are the updates that were applied:';
if ( ! empty( $successful_updates ) ) {
$email['body'] .= "nn" . __( 'Successful updates:' );
foreach ( $successful_updates as $item ) {
$email['body'] .= "n" . '- ' . $item->name;
}
}
if ( ! empty( $failed_updates ) ) {
$email['body'] .= "nn" . __( 'Failed updates:' );
foreach ( $failed_updates as $item ) {
$email['body'] .= "n" . '- ' . $item->name;
}
}
return $email;
}
原文内容
Filters the email sent following an automatic background update for plugins and themes.
Parameters
$emailarray$typestring-
The type of email being sent. Can be one of
'success','fail','mixed'. $successful_updatesarray-
A list of updates that succeeded.
$failed_updatesarray-
A list of updates that failed.
Source
$email = apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates );
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
Skip to note 2 content
Noruzzaman
The
apply_filters( 'auto_plugin_theme_update_email', $email, $type, $successful_updates, $failed_updates )filter is used in WordPress to modify the email content that gets sent after an automatic update of plugins or themes.Add the following code to your theme’s
functions.phpfile or in a custom plugin.add_filter( 'auto_plugin_theme_update_email', 'wpdocs_auto_update_email', 10, 4 ); function wpdocs_auto_update_email( $email, $type, $successful_updates, $failed_updates ) { // Customize the subject line $email['subject'] = 'Custom Subject: Updates on Your Site'; // Customize the body content $email['body'] = 'Hello! Here are the updates that were applied:'; if ( ! empty( $successful_updates ) ) { $email['body'] .= "nn" . __( 'Successful updates:' ); foreach ( $successful_updates as $item ) { $email['body'] .= "n" . '- ' . $item->name; } } if ( ! empty( $failed_updates ) ) { $email['body'] .= "nn" . __( 'Failed updates:' ); foreach ( $failed_updates as $item ) { $email['body'] .= "n" . '- ' . $item->name; } } return $email; }