activate_plugin( string $plugin, string $redirect = '', bool $network_wide = false, bool $silent = false ): null|WP_Error
- Since
- 2.5.0, 5.2.0
- Source
wp-admin/includes/plugin.php:641
Attempts activation of plugin in a "sandbox" and redirects on success.
Description
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.{@source 13 1} 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.
Parameters
$pluginstring- Path to the plugin file relative to the plugins directory.
$redirectstringoptional- URL to redirect to.Default:
'' $network_widebooloptional- Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default false.Default:
false $silentbooloptional- Whether to prevent calling activation hooks. Default false.Default:
false
Return
null|WP_Error- Null on success, WP_Error on invalid file.
Hooks fired · 3
3 hooks fire while activate_plugin() runs, in this order:
- do_action( activate_{$plugin} )actionline 703 (+62 into the body)
Fires before a plugin is activated.
- do_action( activated_plugin )actionline 730 (+89 into the body)
Fires after a plugin has been activated.
Uses · 17
- plugin_basename()Gets the basename of a plugin.
- is_multisite()Determines whether Multisite is enabled.
- is_network_only_plugin()Checks for "Network: true" in the plugin header to see if this should be activated only as a network wide plugin. The plugin would also work when Multisite is not enabled.
- get_site_option()Retrieve an option value for the current network based on name of option.
- get_option()Retrieves an option value based on an option name.
- validate_plugin()Validates the plugin path.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- validate_plugin_requirements()Validates the plugin requirements for WordPress version and PHP version.
- wp_redirect()Redirects to another page.
- add_query_arg()Retrieves a modified URL query string.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- plugin_sandbox_scrape()Loads a given plugin attempt to generate errors.
Show all 17
- do_action()Calls the callback functions that have been added to an action hook.
- update_site_option()Updates the value of an option that was already added for the current network.
- update_option()Updates the value of an option that was already added.
- __()Retrieves the translation of $text.
- WP_Error::__construct()Initializes the error.
Used by · 3
- WP_REST_Plugins_Controller::handle_plugin_status()Handle updating a plugin's status.
- activate_plugins()Activates multiple plugins.
- wp_ajax_activate_plugin()Handles activating a plugin via AJAX.
Source
function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) { $plugin = plugin_basename( trim( $plugin ) ); if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) { $network_wide = true; $current = get_site_option( 'active_sitewide_plugins', array() ); $_GET['networkwide'] = 1; // Back compat for plugins looking for this value. } else { $current = get_option( 'active_plugins', array() ); } $valid = validate_plugin( $plugin ); if ( is_wp_error( $valid ) ) { return $valid; } $requirements = validate_plugin_requirements( $plugin ); if ( is_wp_error( $requirements ) ) { return $requirements; } if ( $network_wide && ! isset( $current[ $plugin ] ) || ! $network_wide && ! in_array( $plugin, $current, true ) ) { if ( ! empty( $redirect ) ) { // We'll override this later if the plugin can be included without fatal error. wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ); } ob_start(); // Load the plugin to test whether it throws any errors. plugin_sandbox_scrape( $plugin ); if ( ! $silent ) { /** * Fires before a plugin is activated. * * If a plugin is silently activated (such as during an update), * this hook does not fire. * * @since 2.9.0 * * @param string $plugin Path to the plugin file relative to the plugins directory. * @param bool $network_wide Whether to enable the plugin for all sites in the network * or just the current site. Multisite only. Default false. */ do_action( 'activate_plugin', $plugin, $network_wide ); /** * Fires as a specific plugin is being activated. * * This hook is the "activation" hook used internally by register_activation_hook(). * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. * * If a plugin is silently activated (such as during an update), this hook does not fire. * * @since 2.0.0 * * @param bool $network_wide Whether to enable the plugin for all sites in the network * or just the current site. Multisite only. Default false. */ do_action( "activate_{$plugin}", $network_wide ); } if ( $network_wide ) { $current = get_site_option( 'active_sitewide_plugins', array() ); $current[ $plugin ] = time(); update_site_option( 'active_sitewide_plugins', $current ); } else { $current = get_option( 'active_plugins', array() ); $current[] = $plugin; sort( $current ); update_option( 'active_plugins', $current ); } if ( ! $silent ) { /** * Fires after a plugin has been activated. *History
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
5.2.0
Test for WordPress version and PHP version compatibility.from the docblock
2.5.0
Introduced.from the docblock
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-admin/includes/plugin.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.