返回介绍

wpmu_delete_user()

发布于 2017-09-11 11:18:27 字数 3455 浏览 975 评论 0 收藏 0

wpmu_delete_user( int $id )

Delete a user from the network and remove from all sites.


description


参数

$id

(int) (Required) The user ID.


返回值

(bool) True if the user was deleted, otherwise false.


源代码

File: wp-admin/includes/ms.php

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
	 *
	 * @param int $id ID of the user about to be deleted from the network.
	 */
	do_action( 'wpmu_delete_user', $id );

	$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 );

	return true;
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-admin/includes/ms.php: wpmu_delete_user
  • wp-admin/includes/user.php: deleted_user
  • wp-admin/includes/bookmark.php: wp_delete_link()
  • wp-includes/class-wp-user.php: WP_User::__construct()
  • wp-includes/capabilities.php: get_super_admins()
  • wp-includes/plugin.php: do_action()
  • wp-includes/user.php: clean_user_cache()
  • wp-includes/user.php: get_blogs_of_user()
  • wp-includes/post.php: wp_delete_post()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • wp-includes/ms-blogs.php: switch_to_blog()
  • wp-includes/ms-blogs.php: restore_current_blog()
  • wp-includes/wp-db.php: wpdb::get_col()
  • wp-includes/wp-db.php: wpdb::delete()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/meta.php: delete_metadata_by_mid()
  • Show 11 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文