core_upgrade_preamble()
云策文档标注
概述
core_upgrade_preamble() 函数用于在 WordPress 后台显示核心升级的提示信息,包括检查更新、显示通知和列出可用更新。它面向开发者,帮助理解升级流程的实现。
关键要点
- 函数通过 get_core_updates() 获取核心更新信息,并基于 $wp_version 判断是否需要升级。
- 根据更新状态输出不同消息:有更新时显示警告通知,开发版本时提示,最新版本时确认。
- 使用 wp_admin_notice() 显示管理员通知,强调备份重要性,并链接到相关文档。
- 通过 list_core_update() 循环列出所有可用更新,并在特定条件下显示维护模式或版本信息。
- 函数还调用 dismissed_updates() 处理已忽略的更新,确保界面完整性。
代码示例
if ( isset( $updates[0]->version ) && version_compare( $updates[0]->version, $wp_version, '>' ) ) {
echo '';
_e( 'An updated version of WordPress is available.' );
echo '';
$message = sprintf(
__( 'Important: Before updating, please back up your database and files. For help with updates, visit the Updating WordPress documentation page.' ),
__( 'https://developer.wordpress.org/advanced-administration/security/backup/' ),
__( 'https://wordpress.org/documentation/article/updating-wordpress/' )
);
wp_admin_notice(
$message,
array(
'type' => 'warning',
'additional_classes' => array( 'inline' ),
)
);
}注意事项
- 函数依赖于全局变量 $wp_version 和 ABSPATH 常量,确保在正确上下文中调用。
- 使用 _e() 和 __() 进行国际化处理,开发者应避免硬编码文本以支持多语言。
- 维护模式通知仅在多个更新或非 'latest' 响应时显示,需注意条件逻辑。
原文内容
Display upgrade WordPress for downloading latest or upgrading automatically form.
Source
function core_upgrade_preamble() {
$updates = get_core_updates();
// Include an unmodified $wp_version.
require ABSPATH . WPINC . '/version.php';
$is_development_version = preg_match( '/alpha|beta|RC/', $wp_version );
if ( isset( $updates[0]->version ) && version_compare( $updates[0]->version, $wp_version, '>' ) ) {
echo '<h2 class="response">';
_e( 'An updated version of WordPress is available.' );
echo '</h2>';
$message = sprintf(
/* translators: 1: Documentation on WordPress backups, 2: Documentation on updating WordPress. */
__( '<strong>Important:</strong> Before updating, please <a href="%1$s">back up your database and files</a>. For help with updates, visit the <a href="%2$s">Updating WordPress</a> documentation page.' ),
__( 'https://developer.wordpress.org/advanced-administration/security/backup/' ),
__( 'https://wordpress.org/documentation/article/updating-wordpress/' )
);
wp_admin_notice(
$message,
array(
'type' => 'warning',
'additional_classes' => array( 'inline' ),
)
);
} elseif ( $is_development_version ) {
echo '<h2 class="response">' . __( 'You are using a development version of WordPress.' ) . '</h2>';
} else {
echo '<h2 class="response">' . __( 'You have the latest version of WordPress.' ) . '</h2>';
}
echo '<ul class="core-updates">';
foreach ( (array) $updates as $update ) {
echo '<li>';
list_core_update( $update );
echo '</li>';
}
echo '</ul>';
// Don't show the maintenance mode notice when we are only showing a single re-install option.
if ( $updates && ( count( $updates ) > 1 || 'latest' !== $updates[0]->response ) ) {
echo '<p>' . __( 'While your site is being updated, it will be in maintenance mode. As soon as your updates are complete, this mode will be deactivated.' ) . '</p>';
} elseif ( ! $updates ) {
list( $normalized_version ) = explode( '-', $wp_version );
echo '<p>' . sprintf(
/* translators: 1: URL to About screen, 2: WordPress version. */
__( '<a href="%1$s">Learn more about WordPress %2$s</a>.' ),
esc_url( self_admin_url( 'about.php' ) ),
$normalized_version
) . '</p>';
}
dismissed_updates();
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |