Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Plugin which runs a function to activate other plugins when new sites are created.
<?php
/*
Plugin Name: Multisite Site Creation Activation Plugin
Plugin URI:
Description: Plugin which runs a function to activate other plugins when new sites are created.
Version: 1.0
Author: Mark Wilkinson
Author URI: http://markwilkinson.me
License: GPLv2 or later
*/
/**************************************************************************
* Function mdw_plugin_actions_on_signup()
* Activate plugins on site creation depending on site type.
**************************************************************************/
function mdw_plugin_actions_on_signup( $blog_id, $user_id ) {
/* get the site type from the user meta */
$mdw_user_site_type = get_user_meta( $user_id, 'mdw_site_type', true );
/* switch to the new blog that has been created */
switch_to_blog( $blog_id );
/* update this sites option for site type to the site type of the user */
update_option( 'mdw_site_type', $mdw_user_site_type );
/* get the site type option */
$mdw_site_type = get_option( 'mdw_site_type' );
/* get a list of the active plugins */
$mdw_active_plugins = get_option( 'active_plugins' );
/* create an array to store our plugins to activate in */
$mdw_plugins_to_activate = array();
/* switch depending on the site type */
switch( $mdw_site_type ) {
/* if this is a bronze site */
case 'bronze':
/* add plugins we want to activate to our array - in this case the bronze plugin */
$mdw_plugins_to_activate[] = 'mdw-bronze/mdw-bronze.php';
break;
case 'silver':
/* add plugins we want to activate to our array - in this case the silver plugin */
$mdw_plugins_to_activate[] = 'mdw-silver/mdw-silver.php';
break;
} // end switch statement
/* loop through each plugin we need to activate */
foreach ( $mdw_plugins_to_activate as $mdw_plugin ) {
/* check whether the plugin we want to activate is not already in the active list */
if( ! in_array( $mdw_plugin, $mdw_active_plugins ) ) {
/* no already active so lets push our plugins to activate into the active plugins array */
array_push( $mdw_active_plugins, $mdw_plugin );
/* update the option for active plugins to include our new plugins */
update_option( 'active_plugins', $mdw_active_plugins );
} // end if plugin not already active
} // end loop through plugins
/* restore the db context back to the original blog */
restore_current_blog();
}
add_action( 'wpmu_new_blog', 'mdw_plugin_actions_on_signup', 10, 2 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment