Trigger plugin activation on new site in a WordPress Network

Allot of plugin have a function that is triggered when the plugin is activated. This ‘activation’ function allows the plugin to setup itself, e.g. create a custom table or set some options in the options table. This works fine in a ‘normal’ WordPress website, but when you create a new site in a WordPress Network website these plugin activation functions are not triggered. In allot of cases we do want these activation functions to run because we want the network activated plugins to ‘install’ themselves for each website.

The following code will trigger the activation function for all network activated plugin, add it to one of you network activated plugins.

function activate_sitewide_plugins( $blog_id )
{
		// Switch to new website
		switch_to_blog( $blog_id );

		// Activate
		foreach( array_keys( get_site_option( 'active_sitewide_plugins' ) ) as $plugin ) {
			do_action( 'activate_' 	. $plugin, false );
			do_action( 'activate' 	. '_plugin', $plugin, false );
		}

		// Restore current website
		restore_current_blog();
}

add_action( 'wpmu_new_blog', 'activate_sitewide_plugins' );

The activation functions of all network activated plugins will now be triggered when a new site is added. If an error occurs in one of the plugins it’s most likely it’s not using the proper wpdb prefix variable.

Related Posts

Powered By Related Posts for WordPress
Click Here to Learn More About Related Posts for WordPress

One thought on “Trigger plugin activation on new site in a WordPress Network

  1. […] Trigger plugin activation on new site in a WordPress Network » […]

Leave a Reply

Your email address will not be published. Required fields are marked *