Skip to content

Instantly share code, notes, and snippets.

@sododesign
Created May 2, 2017 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sododesign/b9d00b9d3f2efd4f1871047d8fdbe744 to your computer and use it in GitHub Desktop.
Save sododesign/b9d00b9d3f2efd4f1871047d8fdbe744 to your computer and use it in GitHub Desktop.
WordPress Multisite: Automatically enable plugins in new blogs
<?php
// There are three options (that I know of) for automatically enabling a plugin
// in new sites.
// 1. Move the plugin from wp-content/plugins/ to wp-content/mu-plugins/ (MU =
// Must Use). But then it cannot be deactivated for any site.
// 2. Click "Network Activate" instead of "Activate" to enable it for all sites.
// I didn't want to use this though because I didn't want to affect existing
// sites. Also I couldn't work out how to deactivate it for some sites (although
// I've read this should be possible).
// 3. Add the following to wp-content/mu-plugins/ to activate the plugin at
// creation time. This way it only affects new blogs, can be deactivated easily,
// and I can also set the default configuration at the same time.
// Helper to activate a plugin on another site without causing a fatal error by
// including the plugin file a second time
// Based on activate_plugin() in wp-admin/includes/plugin.php
// $buffer option is used for All in One SEO Pack, which sends output otherwise
function djm_activate_plugin($plugin, $buffer = false)
{
$current = get_option('active_plugins', array());
if (!in_array($plugin, $current)) {
if ($buffer)
ob_start();
include_once(WP_PLUGIN_DIR . '/' . $plugin);
do_action('activate_plugin', $plugin);
do_action('activate_' . $plugin);
$current[] = $plugin;
sort($current);
update_option('active_plugins', $current);
do_action('activated_plugin', $plugin);
if ($buffer)
ob_end_clean();
}
}
add_action('wpmu_new_blog', 'djm_wpmu_new_blog', 12, 6); // includes/ms-functions.php
function djm_wpmu_new_blog($blog_id, $user_id, $domain, $path, $site_id, $meta)
{
switch_to_blog($blog_id);
// Activate All in One SEO Pack
djm_activate_plugin('all-in-one-seo-pack/all_in_one_seo_pack.php', true);
// Configure All in One SEO Pack
$options = get_option('aioseop_options', array());
$options['aiosp_enabled'] = '1';
$options['aiosp_paged_format'] = ' - Page %page%';
$options['aiosp_cap_cats'] = '0';
update_option('aioseop_options', $options);
restore_current_blog();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment