0

Programmatically, I want to activate / deactivate a plugin of this specific blog/site in a WordPress Multisite. Any help will be gladly appreciated, thank you!

2 Answers 2

2

Use a combination of switch_to_blog(), activate_plugins(), and deactivate_plugins().

You can put this in functions.php, but you'll want to remove it once it runs:

// hook to admin init
add_action('admin_init', 'wpse_swap_plugins');
function wpse_swap_plugins() {
    // set blog ID per your needs
    $blog_id = 3;
    // switch to the site where you want to activate/deactivate plugins
    switch_to_blog($blog_id);
    // activate: set your path, don't set redirect,
    // don't make this network wide, don't prevent activation hooks
    activate_plugins(array(
        '/full/path/to/plugin/you/want/to/activate.php'
    ), '', false, false);
    // deactivate: same arguments as activate
    deactivate_plugins(array(
        '/full/path/to/plugin/to/deactivate.php'
    ), '', false, false);
    // switch context back to original site
    restore_current_blog();
}

Credit to related answer: Deactivate plugin for a specific user group

2
  • You can also use is_plugin_active() to check if the plugin is active before you activate/deactivate it.
    – Pat J
    Oct 17, 2017 at 14:03
  • This is well explained. Thank you so much guys! How about for theme? Programmatically Activate / Deactivate the theme of a Specific Blog
    – Yves
    Oct 17, 2017 at 15:11
0

Via WP-CLI

Reference: https://developer.wordpress.org/cli/commands/plugin/activate/

Syntax:

wp plugin activate <plugin(s)> --url=<url>

Example:

  1. wp plugin activate akismet --url="foo.example.com"
  2. wp plugin activate akismet jetpack ninja-forms --url="bar.example.com"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.