activate_plugin

Attempts activation of plugin in a "sandbox" and redirects on success.

Description

(WP_Error|null) activate_plugin( (string) $plugin, (string) $redirect = '', (constant) $network_wide = false, (bool) $silent = false ); 

A plugin that is already activated will not attempt to be activated again.

The way it works is by setting the redirection to the error before trying to include the plugin file. If the plugin fails, then the redirection will not be overwritten with the success message. Also, the options will not be updated and the activation hook will not be called on plugin error.

It should be noted that in no way the below code will actually prevent errors within the file. The code should not be used elsewhere to replicate the "sandbox", which uses redirection to work.

If any errors are found or text is outputted, then it will be captured to ensure that the success redirection will update the error redirection.

Returns (WP_Error|null)

WP_Error on invalid file or null on success.

Parameters (4)

0. $plugin (string)
Plugin path to main plugin file with plugin data.
1. $redirect — Optional. (string) => ''
URL to redirect to.
2. $network_wide — Optional. (constant) => false
Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default false.
3. $silent — Optional. (bool) => false
Whether to prevent calling activation hooks. Default false.

Usage

  1. if ( !function_exists( 'activate_plugin' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // Plugin path to main plugin file with plugin data. 
  5. $plugin = ''; 
  6.  
  7. // Optional. URL to redirect to. 
  8. $redirect = ''; 
  9.  
  10. // Optional. Whether to enable the plugin for all sites in the network 
  11. // or just the current site. Multisite only. Default false. 
  12. $network_wide = false; 
  13.  
  14. // Optional. Whether to prevent calling activation hooks. Default false. 
  15. $silent = false; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = activate_plugin($plugin, $redirect, $network_wide, $silent); 
  19.  

Defined (1)

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

/wp-admin/includes/plugin.php  
  1. function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { 
  2. $plugin = plugin_basename( trim( $plugin ) ); 
  3.  
  4. if ( is_multisite() && ( $network_wide || is_network_only_plugin($plugin) ) ) { 
  5. $network_wide = true; 
  6. $current = get_site_option( 'active_sitewide_plugins', array() ); 
  7. $_GET['networkwide'] = 1; // Back compat for plugins looking for this value. 
  8. } else { 
  9. $current = get_option( 'active_plugins', array() ); 
  10.  
  11. $valid = validate_plugin($plugin); 
  12. if ( is_wp_error($valid) ) 
  13. return $valid; 
  14.  
  15. if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) { 
  16. if ( !empty($redirect) ) 
  17. wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error 
  18. ob_start(); 
  19. $_wp_plugin_file = $plugin; 
  20. include_once( WP_PLUGIN_DIR . '/' . $plugin ); 
  21. $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin. 
  22.  
  23. if ( ! $silent ) { 
  24. /** 
  25. * Fires before a plugin is activated. 
  26. * If a plugin is silently activated (such as during an update),  
  27. * this hook does not fire. 
  28. * @since 2.9.0 
  29. * @param string $plugin Plugin path to main plugin file with plugin data. 
  30. * @param bool $network_wide Whether to enable the plugin for all sites in the network 
  31. * or just the current site. Multisite only. Default is false. 
  32. */ 
  33. do_action( 'activate_plugin', $plugin, $network_wide ); 
  34.  
  35. /** 
  36. * Fires as a specific plugin is being activated. 
  37. * This hook is the "activation" hook used internally by 
  38. * {@see register_activation_hook()}. The dynamic portion of the 
  39. * hook name, `$plugin`, refers to the plugin basename. 
  40. * If a plugin is silently activated (such as during an update),  
  41. * this hook does not fire. 
  42. * @since 2.0.0 
  43. * @param bool $network_wide Whether to enable the plugin for all sites in the network 
  44. * or just the current site. Multisite only. Default is false. 
  45. */ 
  46. do_action( 'activate_' . $plugin, $network_wide ); 
  47.  
  48. if ( $network_wide ) { 
  49. $current = get_site_option( 'active_sitewide_plugins', array() ); 
  50. $current[$plugin] = time(); 
  51. update_site_option( 'active_sitewide_plugins', $current ); 
  52. } else { 
  53. $current = get_option( 'active_plugins', array() ); 
  54. $current[] = $plugin; 
  55. sort($current); 
  56. update_option('active_plugins', $current); 
  57.  
  58. if ( ! $silent ) { 
  59. /** 
  60. * Fires after a plugin has been activated. 
  61. * If a plugin is silently activated (such as during an update),  
  62. * this hook does not fire. 
  63. * @since 2.9.0 
  64. * @param string $plugin Plugin path to main plugin file with plugin data. 
  65. * @param bool $network_wide Whether to enable the plugin for all sites in the network 
  66. * or just the current site. Multisite only. Default is false. 
  67. */ 
  68. do_action( 'activated_plugin', $plugin, $network_wide ); 
  69.  
  70. if ( ob_get_length() > 0 ) { 
  71. $output = ob_get_clean(); 
  72. return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output); 
  73. ob_end_clean(); 
  74.  
  75. return null;