wpmu_delete_blog

Delete a site.

Description

wpmu_delete_blog( (int) $blog_id, (bool) $drop = false ); 

Parameters (2)

0. $blog_id (int)
The blog id.
1. $drop — Optional. (bool) => false
True if site's database tables should be dropped. Default is false.

Usage

  1. if ( !function_exists( 'wpmu_delete_blog' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ms.php'; 
  3.  
  4. // The blog id. 
  5. $blog_id = -1; 
  6.  
  7. // True if site's database tables should be dropped. Default is false. 
  8. $drop = false; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wpmu_delete_blog($blog_id, $drop); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ms.php  
  1. function wpmu_delete_blog( $blog_id, $drop = false ) { 
  2. global $wpdb; 
  3.  
  4. $switch = false; 
  5. if ( get_current_blog_id() != $blog_id ) { 
  6. $switch = true; 
  7. switch_to_blog( $blog_id ); 
  8.  
  9. $blog = get_site( $blog_id ); 
  10. /** 
  11. * Fires before a site is deleted. 
  12. * @since MU 
  13. * @param int $blog_id The site ID. 
  14. * @param bool $drop True if site's table should be dropped. Default is false. 
  15. */ 
  16. do_action( 'delete_blog', $blog_id, $drop ); 
  17.  
  18. $users = get_users( array( 'blog_id' => $blog_id, 'fields' => 'ids' ) ); 
  19.  
  20. // Remove users from this blog. 
  21. if ( ! empty( $users ) ) { 
  22. foreach ( $users as $user_id ) { 
  23. remove_user_from_blog( $user_id, $blog_id ); 
  24.  
  25. update_blog_status( $blog_id, 'deleted', 1 ); 
  26.  
  27. $current_network = get_network(); 
  28.  
  29. // If a full blog object is not available, do not destroy anything. 
  30. if ( $drop && ! $blog ) { 
  31. $drop = false; 
  32.  
  33. // Don't destroy the initial, main, or root blog. 
  34. if ( $drop && ( 1 == $blog_id || is_main_site( $blog_id ) || ( $blog->path == $current_network->path && $blog->domain == $current_network->domain ) ) ) { 
  35. $drop = false; 
  36.  
  37. $upload_path = trim( get_option( 'upload_path' ) ); 
  38.  
  39. // If ms_files_rewriting is enabled and upload_path is empty, wp_upload_dir is not reliable. 
  40. if ( $drop && get_site_option( 'ms_files_rewriting' ) && empty( $upload_path ) ) { 
  41. $drop = false; 
  42.  
  43. if ( $drop ) { 
  44. $uploads = wp_get_upload_dir(); 
  45.  
  46. $tables = $wpdb->tables( 'blog' ); 
  47. /** 
  48. * Filters the tables to drop when the site is deleted. 
  49. * @since MU 
  50. * @param array $tables The site tables to be dropped. 
  51. * @param int $blog_id The ID of the site to drop tables for. 
  52. */ 
  53. $drop_tables = apply_filters( 'wpmu_drop_tables', $tables, $blog_id ); 
  54.  
  55. foreach ( (array) $drop_tables as $table ) { 
  56. $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); 
  57.  
  58. $wpdb->delete( $wpdb->blogs, array( 'blog_id' => $blog_id ) ); 
  59.  
  60. /** 
  61. * Filters the upload base directory to delete when the site is deleted. 
  62. * @since MU 
  63. * @param string $uploads['basedir'] Uploads path without subdirectory. @see wp_upload_dir() 
  64. * @param int $blog_id The site ID. 
  65. */ 
  66. $dir = apply_filters( 'wpmu_delete_blog_upload_dir', $uploads['basedir'], $blog_id ); 
  67. $dir = rtrim( $dir, DIRECTORY_SEPARATOR ); 
  68. $top_dir = $dir; 
  69. $stack = array($dir); 
  70. $index = 0; 
  71.  
  72. while ( $index < count( $stack ) ) { 
  73. // Get indexed directory from stack 
  74. $dir = $stack[$index]; 
  75.  
  76. $dh = @opendir( $dir ); 
  77. if ( $dh ) { 
  78. while ( ( $file = @readdir( $dh ) ) !== false ) { 
  79. if ( $file == '.' || $file == '..' ) 
  80. continue; 
  81.  
  82. if ( @is_dir( $dir . DIRECTORY_SEPARATOR . $file ) ) { 
  83. $stack[] = $dir . DIRECTORY_SEPARATOR . $file; 
  84. } elseif ( @is_file( $dir . DIRECTORY_SEPARATOR . $file ) ) { 
  85. @unlink( $dir . DIRECTORY_SEPARATOR . $file ); 
  86. @closedir( $dh ); 
  87. $index++; 
  88.  
  89. $stack = array_reverse( $stack ); // Last added dirs are deepest 
  90. foreach ( (array) $stack as $dir ) { 
  91. if ( $dir != $top_dir) 
  92. @rmdir( $dir ); 
  93.  
  94. clean_blog_cache( $blog ); 
  95.  
  96. if ( $switch )