wp_uninitialize_site( int|WP_Site $site_id )
Runs the uninitialization routine for a given site.
Description
This process includes dropping the site’s database tables and deleting its uploads directory.
Parameters
- $site_id
-
(int|WP_Site) (Required) Site ID or object.
Return
(true|WP_Error) True on success, or error object on failure.
Source
File: wp-includes/ms-site.php
function wp_uninitialize_site( $site_id ) { global $wpdb; if ( empty( $site_id ) ) { return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) ); } $site = get_site( $site_id ); if ( ! $site ) { return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) ); } if ( ! wp_is_site_initialized( $site ) ) { return new WP_Error( 'site_already_uninitialized', __( 'The site appears to be already uninitialized.' ) ); } $users = get_users( array( 'blog_id' => $site->id, 'fields' => 'ids', ) ); // Remove users from the site. if ( ! empty( $users ) ) { foreach ( $users as $user_id ) { remove_user_from_blog( $user_id, $site->id ); } } $switch = false; if ( get_current_blog_id() !== $site->id ) { $switch = true; switch_to_blog( $site->id ); } $uploads = wp_get_upload_dir(); $tables = $wpdb->tables( 'blog' ); /** * Filters the tables to drop when the site is deleted. * * @since MU (3.0.0) * * @param string[] $tables Array of names of the site tables to be dropped. * @param int $site_id The ID of the site to drop tables for. */ $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $site->id ); foreach ( (array) $drop_tables as $table ) { $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared } /** * Filters the upload base directory to delete when the site is deleted. * * @since MU (3.0.0) * * @param string $basedir Uploads path without subdirectory. @see wp_upload_dir() * @param int $site_id The site ID. */ $dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $site->id ); $dir = rtrim( $dir, DIRECTORY_SEPARATOR ); $top_dir = $dir; $stack = array( $dir ); $index = 0; while ( $index < count( $stack ) ) { // Get indexed directory from stack. $dir = $stack[ $index ]; // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged $dh = @opendir( $dir ); if ( $dh ) { $file = @readdir( $dh ); while ( false !== $file ) { if ( '.' === $file || '..' === $file ) { $file = @readdir( $dh ); continue; } if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) { $stack[] = $dir . DIRECTORY_SEPARATOR . $file; } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) { @unlink( $dir . DIRECTORY_SEPARATOR . $file ); } $file = @readdir( $dh ); } @closedir( $dh ); } $index++; } $stack = array_reverse( $stack ); // Last added directories are deepest. foreach ( (array) $stack as $dir ) { if ( $dir != $top_dir ) { @rmdir( $dir ); } } // phpcs:enable WordPress.PHP.NoSilencedErrors.Discouraged if ( $switch ) { restore_current_blog(); } return true; }
Changelog
Version | Description |
---|---|
5.1.0 | Introduced. |
© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_uninitialize_site