Advertisements

wp_delete_user

Definition:
function wp_delete_user( $id, $reassign = 'novalue' ) {}

Remove user and optionally reassign posts and links to another user.
If the $reassign parameter is not assigned to an User ID, then all posts will be deleted of that user. The action ‘delete_user’ that is passed the User ID being deleted will be run after the posts are either reassigned or deleted. The user meta will also be deleted that are for that User ID.

Parameters

  • int $id: User ID.
  • int $reassign: Optional. Reassign posts and links to new User ID.

Return values

returns:True when finished.

Defined actions

  • delete_user
    do_action('delete_user', $id);

Source code

function wp_delete_user( $id, $reassign = 'novalue' ) {

	global $wpdb;



	$id = (int) $id;



	// allow for transaction statement

	do_action('delete_user', $id);



	if ( 'novalue' === $reassign || null === $reassign ) {

		$post_ids = $wpdb->get_col( $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id) );



		if ( $post_ids ) {

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

		}

	} else {

		$reassign = (int) $reassign;

		$wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );

		$wpdb->update( $wpdb->links, array('link_owner' => $reassign), array('link_owner' => $id) );

	}



	clean_user_cache($id);



	// FINALLY, delete user

	if ( !is_multisite() ) {

		$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d", $id) );

		$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->users WHERE ID = %d", $id) );

	} else {

		$level_key = $wpdb->get_blog_prefix() . 'capabilities'; // wpmu site admins don't have user_levels

		$wpdb->query("DELETE FROM $wpdb->usermeta WHERE user_id = $id AND meta_key = '{$level_key}'");

	}

3601

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: