wp_dashboard_site_health()
概述
wp_dashboard_site_health() 函数用于在 WordPress 仪表盘中显示站点健康状态小部件。它通过获取并解析缓存的健康检查结果,展示问题数量和状态,并提供链接到站点健康屏幕。
关键要点
- 函数 wp_dashboard_site_health() 显示站点健康状态小部件,基于 get_transient('health-check-site-status-result') 获取数据。
- 处理问题计数数组,包括 'good'、'recommended' 和 'critical' 类别,计算总问题数。
- 根据问题状态动态输出内容:无问题时提示运行检查,有问题时显示数量和链接。
- 使用相关函数如 get_transient()、esc_url()、admin_url() 和翻译函数(如 __()、_n())确保安全性和国际化。
代码示例
function wp_dashboard_site_health() {
$get_issues = get_transient( 'health-check-site-status-result' );
$issue_counts = array();
if ( false !== $get_issues ) {
$issue_counts = json_decode( $get_issues, true );
}
if ( ! is_array( $issue_counts ) || ! $issue_counts ) {
$issue_counts = array(
'good' => 0,
'recommended' => 0,
'critical' => 0,
);
}
$issues_total = $issue_counts['recommended'] + $issue_counts['critical'];
// 输出 HTML 和条件逻辑
}注意事项
- 函数依赖于 transient 'health-check-site-status-result',需确保健康检查已运行并缓存结果。
- 输出内容包含国际化字符串,使用 __() 和 _n() 函数处理翻译和复数形式。
- 首次引入于 WordPress 5.4.0 版本,开发时需考虑版本兼容性。
Displays the Site Health Status widget.
Source
function wp_dashboard_site_health() {
$get_issues = get_transient( 'health-check-site-status-result' );
$issue_counts = array();
if ( false !== $get_issues ) {
$issue_counts = json_decode( $get_issues, true );
}
if ( ! is_array( $issue_counts ) || ! $issue_counts ) {
$issue_counts = array(
'good' => 0,
'recommended' => 0,
'critical' => 0,
);
}
$issues_total = $issue_counts['recommended'] + $issue_counts['critical'];
?>
<div class="health-check-widget">
<div class="health-check-widget-title-section site-health-progress-wrapper loading hide-if-no-js">
<div class="site-health-progress">
<svg aria-hidden="true" focusable="false" width="100%" height="100%" viewBox="0 0 200 200" version="1.1" xmlns="http://www.w3.org/2000/svg">
<circle r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
<circle id="bar" r="90" cx="100" cy="100" fill="transparent" stroke-dasharray="565.48" stroke-dashoffset="0"></circle>
</svg>
</div>
<div class="site-health-progress-label">
</div>
</div>
<div class="site-health-details">
<p>
visit the Site Health screen</a> to gather information about your site now.' ),
esc_url( admin_url( 'site-health.php' ) )
);
?>
</p>
<p>
1 ) : ?>
</p>
0 && false !== $get_issues ) : ?>
<p>
%1$d item</strong> on the <a href="%2$s">Site Health screen</a>.',
'Take a look at the <strong>%1$d items</strong> on the <a href="%2$s">Site Health screen</a>.',
$issues_total
),
$issues_total,
esc_url( admin_url( 'site-health.php' ) )
);
?>
</p>
</div>
</div>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/dashboard.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/dashboard.php#L1962">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L1962-L2045">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-related" data-nosnippet="true"><h2 id="related" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#related">Related</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table" id="uses"><figure class="wp-block-table "><table><thead><tr><th scope="col">Uses</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/_n/">_n()</a><code>wp-includes/l10n.php
Translates and retrieves the singular or plural form based on the supplied number.
get_transient()wp-includes/option.php
Retrieves the value of a transient.
_e()wp-includes/l10n.php
Displays translated text.
__()wp-includes/l10n.php
Retrieves the translation of $text.
esc_url()wp-includes/formatting.php
Checks and cleans a URL.
admin_url()wp-includes/link-template.php
Retrieves the URL to the admin area for the current site.
Changelog
| Version | Description |
|---|---|
| 5.4.0 | Introduced. |