bb_activate_plugin

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

Description

(WP_Error|null) bb_activate_plugin( (string) $plugin, (string) $redirect = '' ); 

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 (2)

0. $plugin (string)
Plugin path to main plugin file with plugin data.
1. $redirect — Optional. (string) => ''
URL to redirect to.

Usage

  1. if ( !function_exists( 'bb_activate_plugin' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-admin/includes/functions.bb-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. // NOTICE! Understand what this does before running. 
  11. $result = bb_activate_plugin($plugin, $redirect); 
  12.  

Defined (1)

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

/bp-forums/bbpress/bb-admin/includes/functions.bb-plugin.php  
  1. function bb_activate_plugin( $plugin, $redirect = '' ) { 
  2. $active_plugins = (array) bb_get_option( 'active_plugins' ); 
  3. $plugin = bb_plugin_basename( trim( $plugin ) ); 
  4.  
  5. $valid_path = bb_validate_plugin( $plugin ); 
  6. if ( is_wp_error( $valid_path ) ) 
  7. return $valid_path; 
  8.  
  9. if ( in_array( $plugin, $active_plugins ) ) { 
  10. return false; 
  11.  
  12. if ( !empty( $redirect ) ) { 
  13. // We'll override this later if the plugin can be included without fatal error 
  14. wp_redirect( add_query_arg( '_scrape_nonce', bb_create_nonce( 'scrape-plugin_' . $plugin ), $redirect ) );  
  15.  
  16. ob_start(); 
  17. @include( $valid_path ); 
  18. // Add to the active plugins array 
  19. $active_plugins[] = $plugin; 
  20. ksort( $active_plugins ); 
  21. bb_update_option( 'active_plugins', $active_plugins ); 
  22. ob_end_clean(); 
  23.  
  24. return $valid_path;