wp_ajax_install_plugin

Ajax handler for installing a plugin.

Description

wp_ajax_install_plugin(); 

Usage

  1. if ( !function_exists( 'wp_ajax_install_plugin' ) ) { 
  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_plugin(); 
  7.  

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_install_plugin() { 
  2. check_ajax_referer( 'updates' ); 
  3.  
  4. if ( empty( $_POST['slug'] ) ) { 
  5. 'slug' => '',  
  6. 'errorCode' => 'no_plugin_specified',  
  7. 'errorMessage' => __( 'No plugin specified.' ),  
  8. ) ); 
  9.  
  10. $status = array( 
  11. 'install' => 'plugin',  
  12. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  13. ); 
  14.  
  15. if ( ! current_user_can( 'install_plugins' ) ) { 
  16. $status['errorMessage'] = __( 'Sorry, you are not allowed to install plugins on this site.' ); 
  17. wp_send_json_error( $status ); 
  18.  
  19. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); 
  20. include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); 
  21.  
  22. $api = plugins_api( 'plugin_information', array( 
  23. 'slug' => sanitize_key( wp_unslash( $_POST['slug'] ) ),  
  24. 'fields' => array( 
  25. 'sections' => false,  
  26. ),  
  27. ) ); 
  28.  
  29. if ( is_wp_error( $api ) ) { 
  30. $status['errorMessage'] = $api->get_error_message(); 
  31. wp_send_json_error( $status ); 
  32.  
  33. $status['pluginName'] = $api->name; 
  34.  
  35. $skin = new WP_Ajax_Upgrader_Skin(); 
  36. $upgrader = new Plugin_Upgrader( $skin ); 
  37. $result = $upgrader->install( $api->download_link ); 
  38.  
  39. if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 
  40. $status['debug'] = $skin->get_upgrade_messages(); 
  41.  
  42. if ( is_wp_error( $result ) ) { 
  43. $status['errorCode'] = $result->get_error_code(); 
  44. $status['errorMessage'] = $result->get_error_message(); 
  45. wp_send_json_error( $status ); 
  46. } elseif ( is_wp_error( $skin->result ) ) { 
  47. $status['errorCode'] = $skin->result->get_error_code(); 
  48. $status['errorMessage'] = $skin->result->get_error_message(); 
  49. wp_send_json_error( $status ); 
  50. } elseif ( $skin->get_errors()->get_error_code() ) { 
  51. $status['errorMessage'] = $skin->get_error_messages(); 
  52. wp_send_json_error( $status ); 
  53. } elseif ( is_null( $result ) ) { 
  54. global $wp_filesystem; 
  55.  
  56. $status['errorCode'] = 'unable_to_connect_to_filesystem'; 
  57. $status['errorMessage'] = __( 'Unable to connect to the filesystem. Please confirm your credentials.' ); 
  58.  
  59. // Pass through theerrorfrom WP_Filesystem if one was raised. 
  60. if ( $wp_filesystem instanceof WP_Filesystem_Base && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { 
  61. $status['errorMessage'] = esc_html( $wp_filesystem->errors->get_error_message() ); 
  62.  
  63. wp_send_json_error( $status ); 
  64.  
  65. $install_status = install_plugin_install_status( $api ); 
  66. $pagenow = isset( $_POST['pagenow'] ) ? sanitize_key( $_POST['pagenow'] ) : ''; 
  67.  
  68. // If install request is coming from import page, do not return network activation link. 
  69. $plugins_url = ( 'import' === $pagenow ) ? admin_url( 'plugins.php' ) : network_admin_url( 'plugins.php' ); 
  70.  
  71. if ( current_user_can( 'activate_plugins' ) && is_plugin_inactive( $install_status['file'] ) ) { 
  72. $status['activateUrl'] = add_query_arg( array( 
  73. '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $install_status['file'] ),  
  74. 'action' => 'activate',  
  75. 'plugin' => $install_status['file'],  
  76. ), $plugins_url ); 
  77.  
  78. if ( is_multisite() && current_user_can( 'manage_network_plugins' ) && 'import' !== $pagenow ) { 
  79. $status['activateUrl'] = add_query_arg( array( 'networkwide' => 1 ), $status['activateUrl'] ); 
  80.  
  81. wp_send_json_success( $status );