wp_ajax_install_theme

Ajax handler for installing a theme.

Description

wp_ajax_install_theme(); 

Usage

  1. if ( !function_exists( 'wp_ajax_install_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_install_theme(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_install_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. $slug = sanitize_key( wp_unslash( $_POST['slug'] ) ); 
  11.  
  12. $status = array( 
  13. 'install' => 'theme',  
  14. 'slug' => $slug,  
  15. ); 
  16.  
  17. if ( ! current_user_can( 'install_themes' ) ) { 
  18. $status['errorMessage'] = __( 'Sorry, you are not allowed to install themes on this site.' ); 
  19. wp_send_json_error( $status ); 
  20.  
  21. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  22. include_once( ABSPATH . 'wp-admin/includes/theme.php' ); 
  23.  
  24. $api = themes_api( 'theme_information', array( 
  25. 'slug' => $slug,  
  26. 'fields' => array( 'sections' => false ),  
  27. ) ); 
  28.  
  29. if ( is_wp_error( $api ) ) { 
  30. $status['errorMessage'] = $api->get_error_message(); 
  31. wp_send_json_error( $status ); 
  32.  
  33. $skin = new WP_Ajax_Upgrader_Skin(); 
  34. $upgrader = new Theme_Upgrader( $skin ); 
  35. $result = $upgrader->install( $api->download_link ); 
  36.  
  37. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  38. $status['debug'] = $skin->get_upgrade_messages(); 
  39.  
  40. if ( is_wp_error( $result ) ) { 
  41. $status['errorCode'] = $result->get_error_code(); 
  42. $status['errorMessage'] = $result->get_error_message(); 
  43. wp_send_json_error( $status ); 
  44. } elseif ( is_wp_error( $skin->result ) ) { 
  45. $status['errorCode'] = $skin->result->get_error_code(); 
  46. $status['errorMessage'] = $skin->result->get_error_message(); 
  47. wp_send_json_error( $status ); 
  48. } elseif ( $skin->get_errors()->get_error_code() ) { 
  49. $status['errorMessage'] = $skin->get_error_messages(); 
  50. wp_send_json_error( $status ); 
  51. } elseif ( is_null( $result ) ) { 
  52. global $wp_filesystem; 
  53.  
  54. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  55. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  56.  
  57. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  58. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  59. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  60.  
  61. wp_send_json_error( $status ); 
  62.  
  63. $status['themeName'] = wp_get_theme( $slug )->get( 'Name' ); 
  64.  
  65. if ( current_user_can( 'switch_themes' ) ) { 
  66. if ( is_multisite() ) { 
  67. $status['activateUrl'] = add_query_arg( array( 
  68. 'action' => 'enable',  
  69. '_wpnonce' => wp_create_nonce( 'enable-theme_' . $slug ),  
  70. 'theme' => $slug,  
  71. ), network_admin_url( 'themes.php' ) ); 
  72. } else { 
  73. $status['activateUrl'] = add_query_arg( array( 
  74. 'action' => 'activate',  
  75. '_wpnonce' => wp_create_nonce( 'switch-theme_' . $slug ),  
  76. 'stylesheet' => $slug,  
  77. ), admin_url( 'themes.php' ) ); 
  78.  
  79. if ( ! is_multisite() && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 
  80. $status['customizeUrl'] = add_query_arg( array( 
  81. 'return' => urlencode( network_admin_url( 'theme-install.php', 'relative' ) ),  
  82. ), wp_customize_url( $slug ) ); 
  83.  
  84. /** 
  85. * See WP_Theme_Install_List_Table::_get_theme_status() if we wanted to check 
  86. * on post-install status. 
  87. */ 
  88. wp_send_json_success( $status );