confirm_delete_users()
云策文档标注
概述
confirm_delete_users() 是 WordPress 多站点用户管理面板中用于在删除用户前显示确认界面的函数。它接收用户 ID 数组作为参数,但实际使用 $_POST['allusers'] 处理,并返回布尔值表示参数有效性。
关键要点
- 函数用途:在多站点用户管理面板中显示删除用户的确认界面,包括用户列表和重新分配选项。
- 参数说明:$users 参数为要删除的用户 ID 数组,但当前未使用,实际依赖 $_POST['allusers']。
- 返回值:如果 $users 不是数组或为空,返回 FALSE;否则返回 TRUE。
- 安全与权限检查:函数会检查当前用户权限、防止删除网络管理员,并处理用户在多站点中的博客重新分配。
- 相关 Hook:do_action( 'delete_user_form', WP_User $current_user, int[] $user_ids ) 在确认按钮前触发。
代码示例
$args = array( 'role' => 'subscriber' );
$subscribers = get_users( $args );
$subscriber_role_ids = array();
foreach ( $subscribers as $user ) {
$subscriber_role_ids[] = $user->ID;
}
confirm_delete_users( $subscriber_role_ids );注意事项
- 参数 $users 目前未使用,实际删除操作基于 $_POST['allusers'],需注意此不一致性。
- 函数包含严格的权限验证,如检查当前用户是否为网络管理员,防止误删关键账户。
- 在多站点环境中,删除用户时需处理博客重新分配,函数提供了相关界面逻辑。
原文内容
Parameters
$usersarrayrequired
Source
function confirm_delete_users( $users ) {
$current_user = wp_get_current_user();
if ( ! is_array( $users ) || empty( $users ) ) {
return false;
}
?>
<h1></h1>
<p></p>
<p></p>
<form action="users.php?action=dodelete" method="post">
<input type="hidden" name="dodelete" />
ID ) . '">' . $current_user->user_login . '</option>';
?>
<table class="form-table" role="presentation">
ID ) ) {
wp_die(
sprintf(
/* translators: %s: User login. */
__( 'Warning! User %s cannot be deleted.' ),
$delete_user->user_login
)
);
}
if ( in_array( $delete_user->user_login, $site_admins, true ) ) {
wp_die(
sprintf(
/* translators: %s: User login. */
__( 'Warning! User cannot be deleted. The user %s is a network administrator.' ),
'<em>' . $delete_user->user_login . '</em>'
)
);
}
?>
<tr>
<th scope="row">user_login; ?>
' . "n"; ?>
</th>
<td><fieldset><p><legend>
' . $delete_user->user_login . '</em>'
);
?>
</legend></p>
$details ) {
$blog_users = get_users(
array(
'blog_id' => $details->userblog_id,
'fields' => array( 'ID', 'user_login' ),
)
);
if ( is_array( $blog_users ) && ! empty( $blog_users ) ) {
$user_site = "<a href='" . esc_url( get_home_url( $details->userblog_id ) ) . "'>{$details->blogname}</a>";
$user_dropdown = '<label for="reassign_user" class="screen-reader-text">' .
/* translators: Hidden accessibility text. */
__( 'Select a user' ) .
'</label>';
$user_dropdown .= "<select name='blog[$user_id][$key]' id='reassign_user'>";
$user_list = '';
foreach ( $blog_users as $user ) {
if ( ! in_array( (int) $user->ID, $allusers, true ) ) {
$user_list .= "<option value='{$user->ID}'>{$user->user_login}</option>";
}
}
if ( '' === $user_list ) {
$user_list = $admin_out;
}
$user_dropdown .= $user_list;
$user_dropdown .= "</select>n";
?>
<ul style="list-style:none;">
<li>
</li>
<li><label><input type="radio" id="delete_option0" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="delete" checked="checked" />
</label></li>
<li><label><input type="radio" id="delete_option1" name="delete[<?php echo $details->userblog_id . '][' . $delete_user->ID; ?>]" value="reassign" />
</label>
</li>
</ul>
</td></tr>';
} else {
?>
<td><p></p></td>
</tr>
</table>
<p></p>
<p></p>
</form>
</pre><p class="wporg-dot-link-list"><a href="https://developer.wordpress.org/reference/files/wp-admin/includes/ms.php/">View all references</a> <a href="https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ms.php#L862">View on Trac</a> <a href="https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ms.php#L862-L1004">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/delete_user_form/"><span class="hook-func">do_action</span>( ‘delete_user_form’, <nobr><span class="arg-type">WP_User</span> <span class="arg-name">$current_user</span></nobr>, <nobr><span class="arg-type">int[]</span> <span class="arg-name">$user_ids</span></nobr> )</a></dt><dd><p>Fires at the end of the delete users form prior to the confirm button.</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</code></td><td><p>Echoes a submit button, with provided text and appropriate class(es).</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/get_super_admins/">get_super_admins()</a><code>wp-includes/capabilities.php</code></td><td><p>Retrieves a list of super admins.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/esc_html_e/">esc_html_e()</a><code>wp-includes/l10n.php</code></td><td><p>Displays translated text that has been escaped for safe use in HTML output.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_get_current_user/">wp_get_current_user()</a><code>wp-includes/pluggable.php</code></td><td><p>Retrieves the current user object.</p>
</td></tr><tr class=""><td><a href="https://developer.wordpress.org/reference/functions/wp_nonce_field/">wp_nonce_field()</a><code>wp-includes/functions.php</code></td><td><p>Retrieves or display nonce hidden field for forms.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_home_url/">get_home_url()</a><code>wp-includes/link-template.php</code></td><td><p>Retrieves the URL for a given site where the front end is accessible.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_blogs_of_user/">get_blogs_of_user()</a><code>wp-includes/user.php</code></td><td><p>Gets the sites a user belongs to.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_users/">get_users()</a><code>wp-includes/user.php</code></td><td><p>Retrieves list of users matching criteria.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/current_user_can/">current_user_can()</a><code>wp-includes/capabilities.php</code></td><td><p>Returns whether the current user has the specified capability.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/_e/">_e()</a><code>wp-includes/l10n.php</code></td><td><p>Displays translated text.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/__/">__()</a><code>wp-includes/l10n.php</code></td><td><p>Retrieves the translation of $text.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/esc_attr/">esc_attr()</a><code>wp-includes/formatting.php</code></td><td><p>Escaping for HTML attributes.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/esc_url/">esc_url()</a><code>wp-includes/formatting.php</code></td><td><p>Checks and cleans a URL.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/get_userdata/">get_userdata()</a><code>wp-includes/pluggable.php</code></td><td><p>Retrieves user info by user ID.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/wp_die/">wp_die()</a><code>wp-includes/functions.php</code></td><td><p>Kills WordPress execution and displays HTML page with an error message.</p>
</td></tr><tr class="wporg-hidden"><td><a href="https://developer.wordpress.org/reference/functions/do_action/">do_action()</a><code>wp-includes/plugin.php</code></td><td><p>Calls the callback functions that have been added to an action hook.</p>
</td></tr></tbody></table></figure><a class="wp-block-wporg-code-table-show-more" href="#">Show 11 more</a><a class="wp-block-wporg-code-table-show-less" href="#">Show less</a></section> </section>
<section class="wp-block-wporg-code-reference-changelog"><h2 id="changelog" class="is-toc-heading wp-block-heading has-heading-5-font-size" tabindex="-1" ><a href="#changelog">Changelog</a></h2> <section style="margin-top:var(--wp--preset--spacing--20)" class="wp-block-wporg-code-table"><figure class="wp-block-table "><table><thead><tr><th scope="col">Version</th><th scope="col">Description</th></tr></thead><tbody><tr class=""><td><a href="https://developer.wordpress.org/reference/since/3.0.0/">3.0.0</a></td><td>Introduced.</td></tr></tbody></table></figure></section> </section>
<section class="wp-block-wporg-code-reference-comments" data-nosnippet="true"><h2 id="user-contributed-notes" class="is-toc-heading wp-block-heading" tabindex="-1" ><a href="#user-contributed-notes">User Contributed Notes</a></h2> <ol class="comment-list"> <li id="comment-3861" data-comment-id="3861" class="comment byuser comment-author-makewebbetter even thread-even depth-1">
<article id="div-comment-3861" class="comment-body">
<a href="#comment-content-3861" class="screen-reader-text">Skip to note 3 content</a>
<header class="comment-meta">
<div class="comment-author vcard">
<span class="comment-author-attribution">
<a href="https://profiles.wordpress.org/makewebbetter/" rel="external nofollow" class="url">MakeWebBetter</a> </span>
<a class="comment-date" href="https://developer.wordpress.org/reference/functions/confirm_delete_users/#comment-3861">
<time datetime="2020-05-11T12:10:47+00:00">
6 years ago </time>
</a>
</div>
<div class="user-note-voting" data-nonce="525fd54fb8" data-can-vote="false"><a class="user-note-voting-up" title="You must log in to vote on the helpfulness of this note" data-id="3861" data-vote="up" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fconfirm_delete_users%2F%23comment-3861"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a><span class="user-note-voting-count " title="100% like this"><span class="screen-reader-text">Vote results for this note: </span>1</span><a class="user-note-voting-down" title="You must log in to vote on the helpfulness of this note" data-id="3861" data-vote="down" href="https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fconfirm_delete_users%2F%23comment-3861"><span class="screen-reader-text">You must log in to vote on the helpfulness of this note</span></a></div> </header>
<!-- .comment-metadata -->
<div class="wporg-has-embedded-code comment-content" id="comment-content-3861">
<p><strong>Delete users who hold subscriber role</strong></p>
<pre class="wp-block-code"><code lang="php" class="language-php line-numbers"> 'subscriber',
'field' => 'ID'
);
$args = array( 'role' => 'subscriber' );
$subscribers = get_users( $args );
$subscriber_role_ids = array();
foreach ( $subscribers as $user ) {
$subscriber_role_ids[] = $user->ID;
}
confirm_delete_users( $subscriber_role_ids );
?>
Codex