delete_theme

Remove a theme.

Description

(void|bool|WP_Error) delete_theme( (string) $stylesheet, (string) $redirect = '' ); 

Returns (void|bool|WP_Error)

When void, echoes content.

Parameters (2)

0. $stylesheet (string)
Stylesheet of the theme to delete
1. $redirect — Optional. (string) => ''
Redirect to page when complete.

Usage

  1. if ( !function_exists( 'delete_theme' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/theme.php'; 
  3.  
  4. // Stylesheet of the theme to delete 
  5. $stylesheet = ''; 
  6.  
  7. // Redirect to page when complete. 
  8. $redirect = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = delete_theme($stylesheet, $redirect); 
  12.  

Defined (1)

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

/wp-admin/includes/theme.php  
  1. function delete_theme($stylesheet, $redirect = '') { 
  2. global $wp_filesystem; 
  3.  
  4. if ( empty($stylesheet) ) 
  5. return false; 
  6.  
  7. if ( empty( $redirect ) ) { 
  8. $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet); 
  9.  
  10. ob_start(); 
  11. $credentials = request_filesystem_credentials( $redirect ); 
  12. $data = ob_get_clean(); 
  13.  
  14. if ( false === $credentials ) { 
  15. if ( ! empty( $data ) ) { 
  16. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  17. echo $data; 
  18. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  19. exit; 
  20. return; 
  21.  
  22. if ( ! WP_Filesystem( $credentials ) ) { 
  23. ob_start(); 
  24. request_filesystem_credentials( $redirect, '', true ); // Failed to connect, Error and request again. 
  25. $data = ob_get_clean(); 
  26.  
  27. if ( ! empty($data) ) { 
  28. include_once( ABSPATH . 'wp-admin/admin-header.php'); 
  29. echo $data; 
  30. include( ABSPATH . 'wp-admin/admin-footer.php'); 
  31. exit; 
  32. return; 
  33.  
  34. if ( ! is_object($wp_filesystem) ) 
  35. return new WP_Error('fs_unavailable', __('Could not access filesystem.')); 
  36.  
  37. if ( is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code() ) 
  38. return new WP_Error('fs_error', __('Filesystem error.'), $wp_filesystem->errors); 
  39.  
  40. // Get the base plugin folder. 
  41. $themes_dir = $wp_filesystem->wp_themes_dir(); 
  42. if ( empty( $themes_dir ) ) { 
  43. return new WP_Error( 'fs_no_themes_dir', __( 'Unable to locate WordPress theme directory.' ) ); 
  44.  
  45. $themes_dir = trailingslashit( $themes_dir ); 
  46. $theme_dir = trailingslashit( $themes_dir . $stylesheet ); 
  47. $deleted = $wp_filesystem->delete( $theme_dir, true ); 
  48.  
  49. if ( ! $deleted ) { 
  50. return new WP_Error( 'could_not_remove_theme', sprintf( __( 'Could not fully remove the theme %s.' ), $stylesheet ) ); 
  51.  
  52. $theme_translations = wp_get_installed_translations( 'themes' ); 
  53.  
  54. // Remove language files, silently. 
  55. if ( ! empty( $theme_translations[ $stylesheet ] ) ) { 
  56. $translations = $theme_translations[ $stylesheet ]; 
  57.  
  58. foreach ( $translations as $translation => $data ) { 
  59. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.po' ); 
  60. $wp_filesystem->delete( WP_LANG_DIR . '/themes/' . $stylesheet . '-' . $translation . '.mo' ); 
  61.  
  62. // Remove the theme from allowed themes on the network. 
  63. if ( is_multisite() ) { 
  64. WP_Theme::network_disable_theme( $stylesheet ); 
  65.  
  66. // Force refresh of theme update information. 
  67. delete_site_transient( 'update_themes' ); 
  68.  
  69. return true;