activate_plugin

Definition:
function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {}

Attempts activation of plugin in a "sandbox" and redirects on success.
A plugin that is already activated will not attempt to be activated again.

Parameters

  • string $plugin: Plugin path to main plugin file with plugin data.
  • string $redirect: Optional. URL to redirect to.
  • bool $network_wide: Whether to enable the plugin for all sites in the network or just the current site. Multisite only. Default is false.
  • bool $silent: Prevent calling activation hooks. Optional, default is false.

Return values

returns:WP_Error on invalid file or null on success.

Defined actions

  • activate_plugin
    do_action( 'activate_plugin', $plugin, $network_wide );
  • activate_’.$plugin
    do_action( 'activate_' . $plugin, $network_wide );
  • activated_plugin
    do_action( 'activated_plugin', $plugin, $network_wide );

Source code

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

	} else {

		$current = get_option( 'active_plugins', array() );

	}



	$valid = validate_plugin($plugin);

	if ( is_wp_error($valid) )

		return $valid;



	if ( !in_array($plugin, $current) ) {

		if ( !empty($redirect) )

			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

		ob_start();

		include_once(WP_PLUGIN_DIR . '/' . $plugin);



		if ( ! $silent ) {

			do_action( 'activate_plugin', $plugin, $network_wide );

			do_action( 'activate_' . $plugin, $network_wide );

		}



		if ( $network_wide ) {

			$current[$plugin] = time();

			update_site_option( 'active_sitewide_plugins', $current );

		} else {

			$current[] = $plugin;

			sort($current);

			update_option('active_plugins', $current);

		}



		if ( ! $silent ) {

			do_action( 'activated_plugin', $plugin, $network_wide );

		}



		if ( ob_get_length() > 0 ) {

			$output = ob_get_clean();

			return new WP_Error('unexpected_output', __('The plugin generated unexpected output.'), $output);

		}

		ob_end_clean();

	}



	return null;

}

201

About these ads

2 Responses to “activate_plugin”

  1. wicked just what I was looking for thank you

  2. Thank you.. i was looking everywhere for this definition. Even the codex didn’t have it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 196 other followers

%d bloggers like this: