delete_plugins

Remove directory and files of a plugin for a list of plugins.

Description

(bool|null|WP_Error) delete_plugins( (array) $plugins, (string) $deprecated = '' ); 

Returns (bool|null|WP_Error)

True on success, false is $plugins is empty, WP_Error on failure. Null if filesystem credentials are required to proceed.

Parameters (2)

0. $plugins (array)
List of plugins to delete.
1. $deprecated — Optional. (string) => ''
The deprecated.

Usage

  1. if ( !function_exists( 'delete_plugins' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // List of plugins to delete. 
  5. $plugins = array(); 
  6.  
  7. // The deprecated. 
  8. $deprecated = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = delete_plugins($plugins, $deprecated); 
  12.  

Defined (1)

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

/wp-admin/includes/plugin.php  
  1. function delete_plugins( $plugins, $deprecated = '' ) { 
  2. global $wp_filesystem; 
  3.  
  4. if ( empty($plugins) ) 
  5. return false; 
  6.  
  7. $checked = array(); 
  8. foreach ( $plugins as $plugin ) 
  9. $checked[] = 'checked[]=' . $plugin; 
  10.  
  11. $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&' . implode('&', $checked), 'bulk-plugins'); 
  12.  
  13. ob_start(); 
  14. $credentials = request_filesystem_credentials( $url ); 
  15. $data = ob_get_clean(); 
  16.  
  17. if ( false === $credentials ) { 
  18. if ( ! empty($data) ) { 
  19. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  20. echo $data; 
  21. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  22. exit; 
  23. return; 
  24.  
  25. if ( ! WP_Filesystem( $credentials ) ) { 
  26. ob_start(); 
  27. request_filesystem_credentials( $url, '', true ); // Failed to connect, Error and request again. 
  28. $data = ob_get_clean(); 
  29.  
  30. if ( ! empty($data) ) { 
  31. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  32. echo $data; 
  33. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  34. exit; 
  35. return; 
  36.  
  37. if ( ! is_object($wp_filesystem) ) 
  38. return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
  39.  
  40. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 
  41. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); 
  42.  
  43. // Get the base plugin folder. 
  44. $plugins_dir = $wp_filesystem->wp_plugins_dir(); 
  45. if ( empty( $plugins_dir ) ) { 
  46. return new WP_Error( 'fs_no_plugins_dir', __( 'Unable to locate WordPress plugin directory.' ) ); 
  47.  
  48. $plugins_dir = trailingslashit( $plugins_dir ); 
  49.  
  50. $plugin_translations = wp_get_installed_translations( 'plugins' ); 
  51.  
  52. $errors = array(); 
  53.  
  54. foreach ( $plugins as $plugin_file ) { 
  55. // Run Uninstall hook. 
  56. if ( is_uninstallable_plugin( $plugin_file ) ) { 
  57. uninstall_plugin($plugin_file); 
  58.  
  59. /** 
  60. * Fires immediately before a plugin deletion attempt. 
  61. * @since 4.4.0 
  62. * @param string $plugin_file Plugin file name. 
  63. */ 
  64. do_action( 'delete_plugin', $plugin_file ); 
  65.  
  66. $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin_file ) ); 
  67.  
  68. // If plugin is in its own directory, recursively delete the directory. 
  69. if ( strpos( $plugin_file, '/' ) && $this_plugin_dir != $plugins_dir ) { //base check on if plugin includes directory separator AND that it's not the root plugin folder 
  70. $deleted = $wp_filesystem->delete( $this_plugin_dir, true ); 
  71. } else { 
  72. $deleted = $wp_filesystem->delete( $plugins_dir . $plugin_file ); 
  73.  
  74. /** 
  75. * Fires immediately after a plugin deletion attempt. 
  76. * @since 4.4.0 
  77. * @param string $plugin_file Plugin file name. 
  78. * @param bool $deleted Whether the plugin deletion was successful. 
  79. */ 
  80. do_action( 'deleted_plugin', $plugin_file, $deleted ); 
  81.  
  82. if ( ! $deleted ) { 
  83. $errors[] = $plugin_file; 
  84. continue; 
  85.  
  86. // Remove language files, silently. 
  87. $plugin_slug = dirname( $plugin_file ); 
  88. if ( '.' !== $plugin_slug && ! empty( $plugin_translations[ $plugin_slug ] ) ) { 
  89. $translations = $plugin_translations[ $plugin_slug ]; 
  90.  
  91. foreach ( $translations as $translation => $data ) { 
  92. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.po' ); 
  93. $wp_filesystem->delete( WP_LANG_DIR . '/plugins/' . $plugin_slug . '-' . $translation . '.mo' ); 
  94.  
  95. // Remove deleted plugins from the plugin updates list. 
  96. if ( $current = get_site_transient('update_plugins') ) { 
  97. // Don't remove the plugins that weren't deleted. 
  98. $deleted = array_diff( $plugins, $errors ); 
  99.  
  100. foreach ( $deleted as $plugin_file ) { 
  101. unset( $current->response[ $plugin_file ] ); 
  102.  
  103. set_site_transient( 'update_plugins', $current ); 
  104.  
  105. if ( ! empty($errors) ) 
  106. return new WP_Error('could_not_remove_plugin', sprintf(__('Could not fully remove the plugin(s) %s.'), implode(', ', $errors)) ); 
  107.  
  108. return true;