Activate WordPress Plugins Automatically via a Function

WordPress stores the active Plugins in the database table options, field activate_plugins, so it is easy to change this value to activate various Plugins by WordPress; either as a Plugin solution after setting up a new installation or because some Plugins need some other Plugins.

I show you as an example a solution. It is important that you don't use the Plugin names, but the string of the file, which is also required in various hooks. Below you will also find a simple solution to get to this string in your backend.

// example on admin init, control about register_activation_hook() 
add_action( 'admin_init', 'fb_activate_plugins' );
// the exmple function
function fb_activate_plugins() {
	
	if ( ! current_user_can('activate_plugins') )
		wp_die(__('You do not have sufficient permissions to activate plugins for this site.'));
	$plugins = FALSE;
	$plugins = get_option('active_plugins'); // get active plugins
	
	if ( $plugins ) {
		// plugins to active
		$pugins_to_active = array(
			'hello.php', // Hello Dolly
			'adminimize/adminimize.php', // Adminimize
			'akismet/akismet.php' // Akismet
		);
		
		foreach ( $pugins_to_active as $plugin ) {
			if ( ! in_array( $plugin, $plugins ) ) {
				array_push( $plugins, $plugin );
				update_option( 'active_plugins', $plugins );
			}
		}
		
	} // end if $plugins

}

The following function and its hook provides a direct output of the string of the Plugin file on the Plugin page in your backend, so please use it only for a quick finding.

add_filter( 'plugin_row_meta', 'fb_get_plugin_string', 10, 4 );
function fb_get_plugin_string( $plugin_meta, $plugin_file, $plugin_data, $status ) {
	// echo plugin file string
	echo '<code>' . $plugin_file . '</code><br>';
	return $plugin_meta;
}

Comments are closed.

7 comments

  1. Eric Mann

    Yes, this would work to immediately activate the plugins, but it's also incredibly dangerous!

    When WordPress activates a plugin, it does so in a sandbox so you don't break your site. If the plugin is broken, you see an error rather than a borked website. If, on the otherhand, you manually change the 'active_plugins' option to include previously untested plugins, you run the risk of immediately breaking your site.

  2. Mike Schinkel

    Nice.

    BTW:

    "$pugins_to_active"

    Typo?

  3. Zane Matthew

    Nice idea, I never thought of doing it that way, but why would you do this?

    If you had dependent plugins you could use: http://wordpress.org/extend/plugins/plugin-dependencies/
    http://core.trac.wordpress.org/ticket/11308

    or you can take the concept scribu has in work it into your existing plugin.

  4. Mike Schinkel

    @Eric,

    Yes, that is a valid concern for a non-technical end-user running a blog but it's not incredibly dangerous for all use-cases.

    For example, if you are building a pre-defined collection of plugins to go with WordPress and a theme and you want to test them for future deployment or new clients or customers that don't require an admin to correctly fiddle with all the settings this would come in handy and be completely appropriate.

    Or if you want to create a setup that you can install automatically so you can run automated unit and/or regression tests then automating settings like this would be critical.

  5. Frank

    @Mike: not typo, its for careful readers. 😉

2 pingbacks

  1. Adventskalender am 13. Dezember « F-LOG-GE
  2. WP Engineer on activating plugins via a function | WPCandy