1

I have a custom plugin that I have written to manage a lot of stuff. I want to use this for the main website.com. I don't want any of the subdomain sites e.g. website.com/site1 to have access to this plugin. I was thinking of implementing something in the plugin itself so it would check the URL and wouldn't do anything if it was not the main website, but this isn't what I really want. I want to completely hide this plugin from all sub websites. I plan on having multiple plugins in the future so I would like to have some control over what sub sites get access to particular plugins.

To further clarify. I'm not looking to give people options I want to decide what options the sub sites have. I have a dirty solution that I do not like.

if(get_site_url() != "http://website.com") exit;

This will cause a fatal exception when the plugin is installed on anything other than the main site, but its really ugly. I want a cleaner way to hide this plugin from the plugin menu for sub sites.

1

Multisite is setup to do this by default. If you simply install the plugin without 'network activating' it, you are able to limit the use to any single site in the network.

To do this, you must be logged in with an account with super-admin rights. Install the plugin on the single-site dashboard > plugins; not from the network admin plugins page.

https://codex.wordpress.org/Multisite_Network_Administration#Plugins

Refer to the Site Specific section for complete details.

  • this does not allow me to block the install of a plugin. I dont want anyone to even have the option to install it. I have a few plugins that are network activated, a few that are not and 1 that i dont want to even show up on any sites other than the primary site – Dan Hastings Dec 14 '15 at 15:09
  • I've done this exact thing on WordPress networks before. There must be a miscommunication here. If you install the plugin, as a super admin, on the single site you want to use it, the other sites in the network will NOT see the code or have access to the plugin. – jdm2112 Dec 14 '15 at 15:19
  • Sub sites are unable to install plugins they must be done by the network admin. I have done this and installed my custom plugin. This plugin is now on the list for the main site and all sub sites. I went to the main site and enabled the plugin, its still visible, but disabled on the sub sites. – Dan Hastings Dec 14 '15 at 15:21
  • Crucial point, they must be done by a network admin, but they can be done on single sites with those rights. If you instal from the network admin menu, it is a network plugin. If you install on a single site's plugins page, as super admin, it is available only to this site. – jdm2112 Dec 14 '15 at 15:39
0

You can filter the active_plugins option and remove the plugin you wish to never be enabled on a site. You'll have to use a mu-plugin though (wp-content/mu-plugins) since filtering the option is too late from within a regular plugin:

add_filter( 'option_active_plugins', function( $plugins ) {
    if ( get_current_blog_id() !== 1 ) {
        $plugin = 'plugin-name/plugin-name.php';
        $key = array_search( $plugin, $plugins );
        if ( $key !== false ) {
            unset( $plugins[ $key ] );
        }
    }

    return $plugins;
});

Note how I'm using get_current_blog_id() here to make sure the logic doesn't break if you decide to add HTTPS to your main site, or change the domain name.

Then, you'll probably want to remove your plugin from the list your users see so they're not confused because nothing will happen when they hit the Activate link:

add_filter( 'all_plugins', function( $plugins ) {
    if ( get_current_blog_id() !== 1 ) {
        $plugin = 'plugin-name/plugin-name.php';
        unset( $plugins[ $plugin ] );
    }

    return $plugins;
});

An alternative approach is to have your plugin be a mu-plugin, i.e. forced and always active on the whole network, and then in your main plugin file simply:

if ( get_current_blog_id() !== 1 )
    return; // Don't execute the rest of this file

// Your plugin stuff goes here

So the file will be parsed on all sites in the network, but execution will be returned immediately.

Hope that helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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