wpmu_delete_blog( int $blog_id, bool $drop = false )
Delete a site.
Parameters
- $blog_id
-
(Required) Site ID.
- $drop
-
(Optional) True if site's database tables should be dropped. Default is false.
Default value: false
Source
File: wp-admin/includes/ms.php
function wpmu_delete_blog( $blog_id, $drop = false ) {
global $wpdb;
$switch = false;
if ( get_current_blog_id() != $blog_id ) {
$switch = true;
switch_to_blog( $blog_id );
}
$blog = get_site( $blog_id );
/**
* Fires before a site is deleted.
*
* @since WP-MU (3.0.0)
*
* @param int $blog_id The site ID.
* @param bool $drop True if site's table should be dropped. Default is false.
*/
do_action( 'delete_blog', $blog_id, $drop );
$users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) );
// Remove users from this blog.
if ( ! empty( $users ) ) {
foreach ( $users as $user_id ) {
remove_user_from_blog( $user_id, $blog_id );
}
}
update_blog_status( $blog_id, 'deleted', 1 );
$current_network = get_network();
// If a full blog object is not available, do not destroy anything.
if ( $drop && ! $blog ) {
$drop = false;
}
// Don't destroy the initial, main, or root blog.
if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_network->path && $blog->domain == $current_network->domain ) ) ) {
$drop = false;
}
$upload_path = trim( get_option( 'upload_path' ) );
// If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable.
if ( $drop && get_site_option( 'ms_files_rewriting' ) && empty( $upload_path ) ) {
$drop = false;
}
if ( $drop ) {
$uploads = wp_get_upload_dir();
$tables = $wpdb->tables( 'blog' );
/**
* Filters the tables to drop when the site is deleted.
*
* @since WP-MU (3.0.0)
*
* @param array $tables The site tables to be dropped.
* @param int $blog_id The ID of the site to drop tables for.
*/
$drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $blog_id );
foreach ( (array) $drop_tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS `$table`" );
}
$wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) );
/**
* Filters the upload base directory to delete when the site is deleted.
*
* @since WP-MU (3.0.0)
*
* @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir()
* @param int $blog_id The site ID.
*/
$dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_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];
$dh = @opendir( $dir );
if ( $dh ) {
while ( ( $file = @readdir( $dh ) ) !== false ) {
if ( $file == '.' || $file == '..' )
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 );
}
}
@closedir( $dh );
}
$index++;
}
$stack = array_reverse( $stack ); // Last added dirs are deepest
foreach ( (array) $stack as $dir ) {
if ( $dir != $top_dir)
@rmdir( $dir );
}
clean_blog_cache( $blog );
}
/**
* Fires after the site is deleted from the network.
*
* @since WP-4.8.0
*
* @param int $blog_id The site ID.
* @param bool $drop True if site's tables should be dropped. Default is false.
*/
do_action( 'deleted_blog', $blog_id, $drop );
if ( $switch )
restore_current_blog();
}
Changelog
Version | Description |
---|---|
WP-3.0.0 | Introduced. |