Activate WordPress Plugins on Site Creation Using Multisite

Have you ever ran a WordPress multisite environment where you have users signing up for accounts and creating sites at the same time? Have you ever wanted to have different types of site level such as Bronze, Silver and Gold with different plugins activated for different levels? Recently I had a similar problem on a project I was working on and therefore set about to find a solution. Below is how I solved the problem of activating specific plugins when a new site is created in a WordPress multisite install.

The project involved users signing up for a site and user account and depending on the level of account or site they choose would depend on the functionality they would have access to. The functionality was provided as different plugins that would be activated for different site levels.

The signup process was handled by the Gravity Forms User Registration add-on, which thankfully also allows you to choose to create a site for the user at the same time. I won’t go into how this works in this tutorial, however there is plenty online and on the Gravity Forms Support site on how to get this setup. For this example we have a form on the front end of the site that allows the someone to sign up for a user account and a site.

One of the important points to this process is that the user must choose their site level. It is important however that this meta data is stored with the site, rather than the user as the sites will inevitably have more than one user. Initially this posed a problem as the signup form provided by the Gravity Forms User Registration Addon only allows data to be stored as User Meta and not in the sites options table. We will get around this later but your form must save the level of site as user meta. In this example our meta key for the site level will be mdw_site_level and the value would be the level of the site, in this case bronze, or silver.

The next stage was to create a function that takes that data processing it and activating the plugins necessary according to the level of site chosen. To do this we need an action hook to hook our function to, that runs right after the site is created. Also this action hook needs to pass the Blog ID of the created site and the User ID of the created user, to our function, so that we can make use of them.

After some digging around there are two hooks which can be used in order to do this. The first is the Gravity Form User Registration add-on hook named gform_site_created. This takes (among others) the site ID of the created site as the user ID of the created user.

The other hook that we could make use of is the WordPress core hook named wpmu_new_blog. This runs after a new site is created in multisite installs and passes a number of arguments but like the hook above it allows us to access to newly created blog ID and the newly created user ID.

So which to use? Well even though I was using the Gravity Forms solution to register the user and create the site, I choose the wpmu_new_blog simply because if I changed later on and used a custom form, the function would still run.

The next issue to resolve was how to activate a plugin programatically without the need to click activate on the plugins list in the WordPress dashboard. After some quick research this is actually very easy to solve. Active plugins are stored in the WordPress options table as an array with the option named active_plugins. Therefore if the plugins path (from within the plugins folder) is contained in that array it is active. Therefore all we need to do is manipulate that array in our plugin, adding the plugins we want to the array and they will be active.

The plugin created is below. See further down for a full explanation of what the plugin does.

https://gist.github.com/wpmark/8510340

Lets run through how our function, hooked to wpmu_new_blog, is going to work as Pseudo code written in plain English.

  1. Get the site type (in this case bronze or silver) from the user meta which was added when the site was created.
  2. Switch the database context to the newly created blog. If we did not do this then we would end up adding the meta data to the main site.
  3. Add an option to the WordPress options table for the newly created blog which is the actual value of the site level brought from user meta (step 1).
  4. Get an array of active plugins in the option active_plugins
  5. Create an empty array which we can put our a list of the plugins we want to activate depending on the site type
  6. Depending on the site type add an array of plugins to add to the empty array we setup in step 5 above
  7. Loop through each of the plugins that we want to activate adding them to the active plugins array we retrieved in step 4
  8. Update the options for active plugins to include our list of additional plugins to active for this site level
  9. Restore the database context back to the blog or site we started on.

So, there you have it a network activated plugin that allows you to control which plugins get activated when a WordPress site is created within a multisite network.

One response

  1. Excellent Work!

    This is exactly what I was looking for my current project.

    Thank you 🙂

Leave a Reply

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