函数文档

wp_network_dashboard_right_now()

💡 云策文档标注

概述

wp_network_dashboard_right_now() 是 WordPress 网络管理仪表板中“Right Now”小部件的核心函数,用于显示网络中的站点和用户统计信息,并提供创建新站点和用户的快捷操作链接。

关键要点

  • 函数检查当前用户权限,若具备 create_sites 或 create_users 能力,则显示相应的创建链接。
  • 使用 get_user_count() 和 get_blog_count() 获取网络中的用户和站点数量,并通过 _n() 和 number_format_i18n() 进行本地化格式化显示。
  • 输出包含站点和用户搜索表单的 HTML 结构,并触发多个 Hook(如 mu_activity_box_end、mu_rightnow_end、wpmuadminresult)以允许插件扩展。
  • 函数在 WordPress 3.1.0 版本中引入,属于网络管理功能的一部分。

代码示例

if ( current_user_can( 'create_sites' ) ) {
    $actions['create-site'] = '' . __( 'Create a New Site' ) . '';
}
if ( current_user_can( 'create_users' ) ) {
    $actions['create-user'] = '' . __( 'Create a New User' ) . '';
}

注意事项

  • 函数输出直接包含 HTML 和表单,适用于仪表板小部件上下文,不应在主题或插件中直接调用,除非需要自定义网络管理界面。
  • 相关函数如 submit_button()、network_admin_url() 和 esc_url() 用于确保安全性和正确性,开发时需注意遵循 WordPress 编码标准。

📄 原文内容

Source

function wp_network_dashboard_right_now() {
$actions = array();

if ( current_user_can( 'create_sites' ) ) {
$actions['create-site'] = '<a href="' . network_admin_url( 'site-new.php' ) . '">' . __( 'Create a New Site' ) . '</a>';
}
if ( current_user_can( 'create_users' ) ) {
$actions['create-user'] = '<a href="' . network_admin_url( 'user-new.php' ) . '">' . __( 'Create a New User' ) . '</a>';
}

$c_users = get_user_count();
$c_blogs = get_blog_count();

/* translators: %s: Number of users on the network. */
$user_text = sprintf( _n( '%s user', '%s users', $c_users ), number_format_i18n( $c_users ) );
/* translators: %s: Number of sites on the network. */
$blog_text = sprintf( _n( '%s site', '%s sites', $c_blogs ), number_format_i18n( $c_blogs ) );

/* translators: 1: Text indicating the number of sites on the network, 2: Text indicating the number of users on the network. */
$sentence = sprintf( __( 'You have %1$s and %2$s.' ), $blog_text, $user_text );

if ( $actions ) {
echo '<ul class="subsubsub">';
foreach ( $actions as $class => $action ) {
$actions[ $class ] = "t<li class='$class'>$action";
}
echo implode( " |</li>n", $actions ) . "</li>n";
echo '</ul>';
}
?>
<br class="clear" />

<p class="youhave"></p>

<form action="<?php echo esc_url( network_admin_url( 'users.php' ) ); ?>" method="get">
<p>
<label class="screen-reader-text" for="search-users">

</label>
<input type="search" name="s" value="" size="30" autocomplete="off" id="search-users" />
'submit_users' ) ); ?>
</p>
</form>

<form action="<?php echo esc_url( network_admin_url( 'sites.php' ) ); ?>" method="get">
<p>
<label class="screen-reader-text" for="search-sites">

</label>
<input type="search" name="s" value="" size="30" autocomplete="off" id="search-sites" />
'submit_sites' ) ); ?>
</p>
</form>
</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#L447">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/dashboard.php#L447-L531">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/mu_activity_box_end/"><span class="hook-func">do_action</span>( ‘mu_activity_box_end’ )</a></dt><dd><p>Fires at the end of the ‘Right Now’ widget in the Network Admin dashboard.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/mu_rightnow_end/"><span class="hook-func">do_action</span>( ‘mu_rightnow_end’ )</a></dt><dd><p>Fires at the end of the ‘Right Now’ widget in the Network Admin dashboard.</p>
</dd><dt class="wp-block-wporg-code-reference-title has-normal-font-size"><a href="https://developer.wordpress.org/reference/hooks/wpmuadminresult/"><span class="hook-func">do_action</span>( ‘wpmuadminresult’ )</a></dt><dd><p>Fires in the Network Admin ‘Right Now’ dashboard widget just before the user and site search form fields.</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/submit_button/">submit_button()</a><code>wp-admin/includes/template.php

Echoes a submit button, with provided text and appropriate class(es).

_n()wp-includes/l10n.php

Translates and retrieves the singular or plural form based on the supplied number.

network_admin_url()wp-includes/link-template.php

Retrieves the URL to the admin area for the network.

get_user_count()wp-includes/user.php

Returns the number of active users in your installation.

get_blog_count()wp-includes/ms-functions.php

Gets the number of active sites on the installation.

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.

_e()wp-includes/l10n.php

Displays translated text.

esc_url()wp-includes/formatting.php

Checks and cleans a URL.

number_format_i18n()wp-includes/functions.php

Converts float number to format based on the locale.

do_action()wp-includes/plugin.php

Calls the callback functions that have been added to an action hook.

Show 6 moreShow less

Changelog

Version Description
3.1.0 Introduced.