activate_plugins

Activate multiple plugins.

Description

(bool|WP_Error) activate_plugins( (string|array) $plugins, (string) $redirect = '', (constant) $network_wide = false, (bool) $silent = false ); 

When WP_Error is returned, it does not mean that one of the plugins had errors. It means that one or more of the plugins file path was invalid.

The execution will be halted as soon as one of the plugins has an error.

Returns (bool|WP_Error)

True when finished or WP_Error if there were errors during a plugin activation.

Parameters (4)

0. $plugins (string|array)
Single plugin or list of plugins to activate.
1. $redirect — Optional. (string) => ''
Redirect to page after successful activation.
2. $network_wide — Optional. (constant) => false
Whether to enable the plugin for all sites in the network.
3. $silent — Optional. (bool) => false
Prevent calling activation hooks. Default is false.

Usage

  1. if ( !function_exists( 'activate_plugins' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // Single plugin or list of plugins to activate. 
  5. $plugins = null; 
  6.  
  7. // Redirect to page after successful activation. 
  8. $redirect = ''; 
  9.  
  10. // Whether to enable the plugin for all sites in the network. 
  11. $network_wide = false; 
  12.  
  13. // Prevent calling activation hooks. Default is false. 
  14. $silent = false; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = activate_plugins($plugins, $redirect, $network_wide, $silent); 
  18.  

Defined (1)

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

/wp-admin/includes/plugin.php  
  1. function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) { 
  2. if ( !is_array($plugins) ) 
  3. $plugins = array($plugins); 
  4.  
  5. $errors = array(); 
  6. foreach ( $plugins as $plugin ) { 
  7. if ( !empty($redirect) ) 
  8. $redirect = add_query_arg('plugin', $plugin, $redirect); 
  9. $result = activate_plugin($plugin, $redirect, $network_wide, $silent); 
  10. if ( is_wp_error($result) ) 
  11. $errors[$plugin] = $result; 
  12.  
  13. if ( !empty($errors) ) 
  14. return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors); 
  15.  
  16. return true;