wp_ajax_delete_theme

Ajax handler for deleting a theme.

Description

wp_ajax_delete_theme(); 

Usage

  1. if ( !function_exists( 'wp_ajax_delete_theme' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_delete_theme(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_delete_theme() { 
  2. check_ajax_referer( 'updates' ); 
  3.  
  4. if ( empty( $_POST['slug'] ) ) { 
  5. 'slug' => '',  
  6. 'errorCode' => 'no_theme_specified',  
  7. 'errorMessage' => __( 'No theme specified.' ),  
  8. ) ); 
  9.  
  10. $stylesheet = preg_replace( '/[^A-z0-9_\-]/', '', wp_unslash( $_POST['slug'] ) ); 
  11. $status = array( 
  12. 'delete' => 'theme',  
  13. 'slug' => $stylesheet,  
  14. ); 
  15.  
  16. if ( ! current_user_can( 'delete_themes' ) ) { 
  17. $status['errorMessage'] = __( 'Sorry, you are not allowed to delete themes on this site.' ); 
  18. wp_send_json_error( $status ); 
  19.  
  20. if ( ! wp_get_theme( $stylesheet )->exists() ) { 
  21. $status['errorMessage'] = __( 'The requested theme does not exist.' ); 
  22. wp_send_json_error( $status ); 
  23.  
  24. // Check filesystem credentials. `delete_theme()` will bail otherwise. 
  25. $url = wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ); 
  26. ob_start(); 
  27. $credentials = request_filesystem_credentials( $url ); 
  28. ob_end_clean(); 
  29. if ( false === $credentials || ! WP_Filesystem( $credentials ) ) { 
  30. global $wp_filesystem; 
  31.  
  32. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  33. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  34.  
  35. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  36. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  37. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  38.  
  39. wp_send_json_error( $status ); 
  40.  
  41. include_once( ABSPATH . 'wp-admin/includes/theme.php' ); 
  42.  
  43. $result = delete_theme( $stylesheet ); 
  44.  
  45. if ( is_wp_error( $result ) ) { 
  46. $status['errorMessage'] = $result->get_error_message(); 
  47. wp_send_json_error( $status ); 
  48. } elseif ( false === $result ) { 
  49. $status['errorMessage'] = __( 'Theme could not be deleted.' ); 
  50. wp_send_json_error( $status ); 
  51.  
  52. wp_send_json_success( $status );