wp_dashboard_right_now()
概述
wp_dashboard_right_now() 是一个 WordPress 仪表盘小工具函数,用于显示网站的基本统计信息,如文章、页面和评论数量。自 WordPress 3.8 起,它被重命名为 'At a Glance',提供更简洁的概览视图。
关键要点
- 该函数生成一个仪表盘小工具,展示发布状态的文章和页面数量、已批准和待审核的评论数。
- 它使用 wp_count_posts() 和 wp_count_comments() 来获取统计数据,并根据用户权限动态生成链接。
- 支持国际化,通过 _n() 和 number_format_i18n() 处理单复数翻译和数字格式化。
- 提供多个 Hook,如 do_action('activity_box_end') 和 apply_filters('dashboard_glance_items'),允许开发者扩展或修改输出内容。
- 函数内部包含条件逻辑,例如检查是否为网络或用户管理员界面,以调整显示内容。
代码示例
function wp_dashboard_right_now() {
// 获取文章和页面数量
$num_posts = wp_count_posts('post');
if ($num_posts && $num_posts->publish) {
$text = _n('%s Post', '%s Posts', $num_posts->publish);
$text = sprintf($text, number_format_i18n($num_posts->publish));
// 输出链接或纯文本,基于用户权限
}
// 获取评论数量
$num_comm = wp_count_comments();
if ($num_comm && ($num_comm->approved || $num_comm->moderated)) {
$text = sprintf(_n('%s Comment', '%s Comments', $num_comm->approved), number_format_i18n($num_comm->approved));
// 输出评论统计
}
// 应用过滤器以添加额外项目
$elements = apply_filters('dashboard_glance_items', array());
if (!empty($elements)) {
echo '<ul><li>' . implode('</li><li>', $elements) . '</li></ul>';
}
}注意事项
- 该函数在 WordPress 2.7.0 中引入,自 3.8 版本起,其显示名称从 'Right Now' 更改为 'At a Glance',但函数名保持不变。
- 输出内容受用户权限影响,例如只有具有编辑权限的用户才能看到文章和页面的编辑链接。
- 开发者可以通过 Hook 如 dashboard_glance_items 添加自定义统计项到小工具中。
- 函数内部使用了多个核心函数,如 current_user_can() 和 get_post_type_object(),确保安全性和正确性。
Dashboard widget that displays some basic stats about the site.
Description
Formerly ‘Right Now’. A streamlined ‘At a Glance’ as of 3.8.
Source
function wp_dashboard_right_now() {
?>
<div class="main">
<ul>
publish ) {
if ( 'post' === $post_type ) {
/* translators: %s: Number of posts. */
$text = _n( '%s Post', '%s Posts', $num_posts->publish );
} else {
/* translators: %s: Number of pages. */
$text = _n( '%s Page', '%s Pages', $num_posts->publish );
}
$text = sprintf( $text, number_format_i18n( $num_posts->publish ) );
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
printf( '<li class="%1$s-count"><a href="edit.php?post_type=%1$s">%2$s</a></li>', $post_type, $text );
} else {
printf( '<li class="%1$s-count"><span>%2$s</span></li>', $post_type, $text );
}
}
}
// Comments.
$num_comm = wp_count_comments();
if ( $num_comm && ( $num_comm->approved || $num_comm->moderated ) ) {
/* translators: %s: Number of comments. */
$text = sprintf( _n( '%s Comment', '%s Comments', $num_comm->approved ), number_format_i18n( $num_comm->approved ) );
?>
<li class="comment-count">
<a href="edit-comments.php"></a>
</li>
moderated );
/* translators: %s: Number of comments. */
$text = sprintf( _n( '%s Comment in moderation', '%s Comments in moderation', $num_comm->moderated ), $moderated_comments_count_i18n );
?>
<li class="comment-mod-count<?php echo ! $num_comm->moderated ? ' hidden' : ''; ?>">
<a href="edit-comments.php?comment_status=moderated" class="comments-in-moderation-text"></a>
</li>
' . implode( "</li>n<li>", $elements ) . "</li>n";
}
?>
</ul>
<a href='options-reading.php'$title_attr>$content</a></p>";
}
?>
</div>
<div class="sub">
</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#L300">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L300-L442">View on GitHub</a></p></section>
<section class="wp-block-wporg-code-reference-hooks"><h2 id="hooks" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#hooks">Hooks</a></h2> <dl><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/activity_box_end/"><span class="hook-func">do_action</span>( ‘activity_box_end’ )</a></dt><dd><p>Fires at the end of the ‘At a Glance’ dashboard widget.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/dashboard_glance_items/"><span class="hook-func">apply_filters</span>( ‘dashboard_glance_items’, <nobr><span class="arg-type">string[]</span> <span class="arg-name">$items</span></nobr> )</a></dt><dd><p>Filters the array of extra elements to list in the ‘At a Glance’ dashboard widget.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/privacy_on_link_text/"><span class="hook-func">apply_filters</span>( ‘privacy_on_link_text’, <nobr><span class="arg-type">string</span> <span class="arg-name">$content</span></nobr> )</a></dt><dd><p>Filters the link label for the ‘Search engines discouraged’ message displayed in the ‘At a Glance’ dashboard widget.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/privacy_on_link_title/"><span class="hook-func">apply_filters</span>( ‘privacy_on_link_title’, <nobr><span class="arg-type">string</span> <span class="arg-name">$title</span></nobr> )</a></dt><dd><p>Filters the link title attribute for the ‘Search engines discouraged’ message displayed in the ‘At a Glance’ dashboard widget.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/rightnow_end/"><span class="hook-func">do_action</span>( ‘rightnow_end’ )</a></dt><dd><p>Fires at the end of the ‘At a Glance’ dashboard widget.</p>
</dd></dl></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/update_right_now_message/">update_right_now_message()</a><code>wp-admin/includes/update.php
Displays WordPress version and active theme in the ‘At a Glance’ dashboard widget.
_n()wp-includes/l10n.php
Translates and retrieves the singular or plural form based on the supplied number.
is_network_admin()wp-includes/load.php
Determines whether the current request is for the network administrative interface.
is_user_admin()wp-includes/load.php
Determines whether the current request is for a user admin screen.
wp_count_posts()wp-includes/post.php
Counts number of posts of a post type and if user has permissions to view.
wp_count_comments()wp-includes/comment.php
Retrieves the total comment counts for the whole site or a single post.
current_user_can()wp-includes/capabilities.php
Returns whether the current user has the specified capability.
__()wp-includes/l10n.php
Retrieves the translation of $text.
number_format_i18n()wp-includes/functions.php
Converts float number to format based on the locale.
apply_filters()wp-includes/plugin.php
Calls the callback functions that have been added to a filter hook.
do_action()wp-includes/plugin.php
Calls the callback functions that have been added to an action hook.
get_option()wp-includes/option.php
Retrieves an option value based on an option name.
get_post_type_object()wp-includes/post.php
Retrieves a post type object by name.
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |