WordPress at a glance

activate_plugins() WP 2.6.0

Activates the specified plugins.

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.

Works in the admin panel only. If you need it on front, then connect the file:

require_once ABSPATH .'/wp-admin/includes/plugin.php';
Works based on: activate_plugin()

No Hooks.

Return

true/false/WP_Error.

  • True when finished.
  • WP_Error if there were errors during a plugin activation (one or more path to plugin file is wrong).

Usage

activate_plugins( $plugins, $redirect, $network_wide, $silent );
$plugins(string/array) (required)
Single plugin (string) or list of plugins (array) to activate. The ID looks like the path to the plugin file relative to the plugins folder: democracy/democracy.php
$redirect(string)
Page URL to redirect to after successful activation.
Default: ''
$network_wide(true/false)
Whether to enable the plugin for all sites in the network (multisite only).
Default: false
$silent(true/false)
Silent activation. true - means to activate the plugin without enabling activation hooks activate_***, i.e. activation hooks will not work.
Default: false

Examples

#1 Activate the specified plugin

//require_once ABSPATH .'/wp-admin/includes/plugin.php';

activate_plugins( 'democracy-poll/democracy.php' );

Code of activate_plugins: wp-admin/includes/plugin.php VER 4.9.8

<?php
function activate_plugins( $plugins, $redirect = '', $network_wide = false, $silent = false ) {
	if ( !is_array($plugins) )
		$plugins = array($plugins);

	$errors = array();
	foreach ( $plugins as $plugin ) {
		if ( !empty($redirect) )
			$redirect = add_query_arg('plugin', $plugin, $redirect);
		$result = activate_plugin($plugin, $redirect, $network_wide, $silent);
		if ( is_wp_error($result) )
			$errors[$plugin] = $result;
	}

	if ( !empty($errors) )
		return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);

	return true;
}

Related Functions

From tag: plugin

More from category: Plugins, Hooks

No comments
    Hello, !     Log In . Register