Only allow plugin to be activated on root site of multisite

I’m developing the wordpress file monitor plus plugin.

Its purpose is to scan for altered files and it works fine for a single installation of WP. But when you look at multi-site it’s not something that wants to be enabled on all sub sites, as they all share the same files and (to be honest) only a network admin should be the one that wants to be notified against file changes.

Read More

So my question is this:

How would I program it, to only be allowed to be installed/run from the root site of a multi-site installation? So: that means not allowing it to be network installed and only activated from the root blog.

Anyone know of any ways on how I could achieve this?

Related posts

Leave a Reply

3 comments

  1. You could check if the constant SITE_ID_CURRENT_SITE matches get_current_site()->id. The following does this for the activation. During runtime you have to check it again.

    register_activation_hook( __FILE__, 'force_main_site_installation' );
    
    function force_main_site_installation()
    {
        if ( defined( 'SITE_ID_CURRENT_SITE' )
            and SITE_ID_CURRENT_SITE !== get_current_site()->id 
        )
        {
            if ( function_exists('deactivate_plugins') )
            {
                deactivate_plugins( __FILE__ );
            }
            die( 'Install this plugin on the main site only.' );
        }
    }
    
  2. First the explanation for the uninstall/activation/deactivation hooks.

    Second: check for is_blog_installed( $blog_ID ); and then look into all the get_blog_whatever(); and get_blogaddress_whatever();` functions (I guess you use phpstorm as IDE, so autocomplete for functions should be available to you).

    Edit: You could also try to restrict the available menu with registering the admin pages with network_admin_menu() (triggers only if is_network_admin()), user_admin_menu(); (triggers only if is_user_admin()) instead of admin_menu();.

  3. Only this solution worked for me :

    register_activation_hook( __FILE__,  function () {
        if ( is_multisite() && ! strpos( $_SERVER['REQUEST_URI'], 'wp-admin/network/plugins.php' ) ) {
            die ( __( '<script>alert("Activate this plugin only from the NETWORK DASHBOARD.");</script>') );
        }
        // ...............
    });