Automatically enable plugins in new WordPress Multisite 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(); | |
} |
This comment has been minimized.
This comment has been minimized.
Ligemer
commented
Jan 8, 2014
Thank you... saved my brain some thinking and code writing. Wonderful solution. @joglomedia - you create a folder in mu-plugins/yourawesomeplugin/ and put this code in a php file in that. But my guess is if you're asking where this goes you might need more help than just that. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
joglomedia commentedOct 14, 2013
Hi, nice solution, it is what I am looking for my multisite install,
where this file should be located on?
Regards