函数文档

wpmu_delete_user()

💡 云策文档标注

概述

wpmu_delete_user() 函数用于从 WordPress 多站点网络中删除指定用户及其所有内容。它会遍历用户所属的所有站点,删除其文章和链接,并从数据库中移除用户记录。

关键要点

  • 删除用户在所有站点上的所有文章(包括所有文章类型)和链接
  • 从所有站点中移除用户,并最终从数据库中删除用户记录
  • 受保护的超级管理员用户无法被删除
  • 函数执行前后会触发 wpmu_delete_user 和 deleted_user 钩子
  • 参数为整数类型的用户 ID,返回布尔值表示删除是否成功

代码示例

$user_id = 2;
if ( ! function_exists( 'wpmu_delete_user' ) ) { 
	require_once ABSPATH . '/wp-admin/includes/ms.php'; 
}

$deleted = wpmu_delete_user( $user_id );

if ( $deleted ) {
	echo "User deleted successfully";
}

注意事项

  • 使用前需确保函数已加载,通常需要包含 wp-admin/includes/ms.php 文件
  • 删除操作不可逆,请谨慎调用,建议先备份数据

📄 原文内容

Deletes a user and all of their posts from the network.

Description

This function:

  • Deletes all posts (of all post types) authored by the user on all sites on the network
  • Deletes all links owned by the user on all sites on the network
  • Removes the user from all sites on the network
  • Deletes the user from the database

Parameters

$idintrequired
The user ID.

Return

bool True if the user was deleted, false otherwise.

Source

function wpmu_delete_user( $id ) {
	global $wpdb;

	if ( ! is_numeric( $id ) ) {
		return false;
	}

	$id   = (int) $id;
	$user = new WP_User( $id );

	if ( ! $user->exists() ) {
		return false;
	}

	// Global super-administrators are protected, and cannot be deleted.
	$_super_admins = get_super_admins();
	if ( in_array( $user->user_login, $_super_admins, true ) ) {
		return false;
	}

	/**
	 * Fires before a user is deleted from the network.
	 *
	 * @since MU (3.0.0)
	 * @since 5.5.0 Added the `$user` parameter.
	 *
	 * @param int     $id   ID of the user about to be deleted from the network.
	 * @param WP_User $user WP_User object of the user about to be deleted from the network.
	 */
	do_action( 'wpmu_delete_user', $id, $user );

	$blogs = get_blogs_of_user( $id );

	if ( ! empty( $blogs ) ) {
		foreach ( $blogs as $blog ) {
			switch_to_blog( $blog->userblog_id );
			remove_user_from_blog( $id, $blog->userblog_id );

			$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
			foreach ( (array) $post_ids as $post_id ) {
				wp_delete_post( $post_id );
			}

			// Clean links.
			$link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );

			if ( $link_ids ) {
				foreach ( $link_ids as $link_id ) {
					wp_delete_link( $link_id );
				}
			}

			restore_current_blog();
		}
	}

	$meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
	foreach ( $meta as $mid ) {
		delete_metadata_by_mid( 'user', $mid );
	}

	$wpdb->delete( $wpdb->users, array( 'ID' => $id ) );

	clean_user_cache( $user );

	/** This action is documented in wp-admin/includes/user.php */
	do_action( 'deleted_user', $id, null, $user );

	return true;
}

Hooks

do_action( ‘deleted_user’, int $id, int|null $reassign, WP_User $user )

Fires immediately after a user is deleted from the site.

do_action( ‘wpmu_delete_user’, int $id, WP_User $user )

Fires before a user is deleted from the network.

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes