activate_plugins() WordPress Function

The activate_plugins() function enables all plugins that have been previously deactivated. This function is useful for reactivating all plugins after a site has been updated.

activate_plugins( string|string[] $plugins, string $redirect = '', bool $network_wide = false, bool $silent = false ) #

Activates multiple plugins.


Description

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

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


Top ↑

Parameters

$plugins

(string|string[])(Required)Single plugin or list of plugins to activate.

$redirect

(string)(Optional)Redirect to page after successful activation.

Default value: ''

$network_wide

(bool)(Optional)Whether to enable the plugin for all sites in the network.

Default value: false

$silent

(bool)(Optional)Prevent calling activation hooks.

Default value: false


Top ↑

Return

(bool|WP_Error) True when finished or WP_Error if there were errors during a plugin activation.


Top ↑

Source

File: wp-admin/includes/plugin.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;
}


Top ↑

Changelog

Changelog
VersionDescription
2.6.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.