Limit total number of plugins to be activated

Limit on how many plugins can be enabled based of of the level. Using Premium Plugin Manager module, and would like to see whether it's possible to limit the total number of plugins that could be activated.

Suppose if you enable 10 plugins via Premium plugins Manager, and you only want the site admins to activate random 5 plugins from that 10 plugins.

  • Panos

    Hi Tyson Buck ,

    This functionality is not supported by Pro Sites plugin and requires some custom coding which exceeds the purpose of this forum. However I prepared a snippet which can help you on this.

    Download it from here: c80d47ccc43167e51248b5147f7eccbd

    This should add a notice in plugins page, also it would remove the activation link from plugins. Additionally, once a plugin gets activated ( eg by some code ), it will deactivate it if blog level limit is exceeded.

    Once you download you will need to modify it a bit. Specifically you will need to change the levels and the number supported by level. Find this part:

    $plugins_per_level = array(
    				0 => 1, //0 not a pro site, no level...
    				1 => 2,
    				2 => 4,
    				3 => '-' //'-' has no limit
    			);

    and change it accordingly. The first numbers is the id of the level, and the second numbers are the number of active plugins allowed for this level. So for level 2 it allows 4 plugins to be active. The '-' has no limit.

    One other thing you may need to change is this part:

    return $blog_plugins + $network_plugins;
    //return $blog_plugins;

    This adds the number of network wide active plugin too. If you want to count only active plugins per blog ( excluding network wide ) change that part to:
    return $blog_plugins;

    Hope this helps

    Cheers!

  • Tyson Buck

    Hi Panos,

    A few questions for you.

    1. I don't seem to see any downloadable file in reference to this here:
    Download it from here: c80d47ccc43167e51248b5147f7eccbd

    2. When you say "also it would remove the activation link from plugins" do you mean the activation link will never show on the plugins? Or. it will remove the activation link after the limit has been reached?

    3. On "Additionally, once a plugin gets activated ( eg by some code )" what do you mean by "(example by some code)"?

    Thank you for all this btw. You are a life saver and all of y'all are greatly appreciated.

    Cheers,
    TB

  • Panos

    Hi Tyson Buck ,

    1. Sorry, the download link is https://gist.github.com/panoslyrakis/c80d47ccc43167e51248b5147f7eccbd

    2. Yes,if the limit has been reached it should remove the "Activate" link from plugins list.

    3. Initially I added the limit check when a plugin got activated, then I though it would be cool to just remove the Activate link. So after adding the part for the Activate link I still kept the previous part, which should not be needed generally. It could be handy only if someone would activate plugin by code, eg by other plugin or theme.

    Sorry again for not including the download link and hope this clears your questions

    Thanks!

  • Tyson Buck

    Hey Panos,

    Thanks for the reply.

    Is there a way to exclude network enabled plugins "and" (code enabled and plugins) from the plugin limit I set?

    Also is there a way to have it to where a super admin or multi site network admin can override a plugin limit set?
    For example I would be able to see the activation link in a Customers subsite and I could enable a plugin without it affecting the plugin limit set?

    Also is there a way for me to add a notification/message (for each pro site level) in the plugins page pop up when the limit has been reached per level?

    Also where do I need to upload this file?

  • Panos

    Hi Tyson Buck ,

    Your requirements exceed the purpose of support as these are very custom requests. I think you can find a developer in the Jobs & Pros forum who could easily modify the snipped I shared to meet you needs.

    If you have some development knowledge and want to try on your own, I can give some instructions

    1.

    Is there a way to exclude network enabled plugins "and" (code enabled and plugins) from the plugin limit I set?

    If you want to exclude the network plugins from the limit you can change the lines that contain:

    return $blog_plugins + $network_plugins;
    //return $blog_plugins;

    with:

    return $blog_plugins;

    2.

    Also is there a way to have it to where a super admin or multi site network admin can override a plugin limit set?

    This is bit tricky. You first need to store in blog options which plugins have been enabled by super admin and then subtract their number in the blog_can_have_more_plugins() function.

    I have updated the gist here
    https://gist.github.com/panoslyrakis/c80d47ccc43167e51248b5147f7eccbd
    which includes what I mention above for both.

    Also is there a way for me to add a notification/message (for each pro site level) in the plugins page pop up when the limit has been reached per level?

    It already adds a notification when limit is exceeded, an you can change it in function admin_notice()

    Currently it should display a message like in the screenshot:

    You should be able to some js here for the popup, or you could add a new action in admin_footer hook where you can do the same check and include the js for the popup. There are many js scripts available for popup which you can try out.

    Hope this helps

    Cheers!

  • Panos

    Hey Tyson Buck ,

    Apologies I missed providing this information

    You can upload it to you site-root/wp-content/mu-plugins folder so it acts as a mu-plugin ( you can read more about mu-plugins here ). If this folder doesn't exist you can simply cerate it, only make sure you don't use any capital letters in the name, it should be "mu-plugins".

    Alternatively you can try installing it as a normal plugin.

    Thanks!

  • Tyson Buck

    Hey Panos,

    I figured out how to install the file.

    I am still a little confused as to how a few snippets needs to be used.

    First this one: blog_can_have_more_plugins()
    Can you explain a little more on how to use this?

    Second is this one:
    admin_notice()
    I have set up a test site on the best level I offer and once I hit the limit it gives the notice to upgrade. However there’s not a higher plan for them to upgrade too.
    So what I would like to do is on that plan instead of asking them to upgrade I would like to ask them to request a higher plan.

    Also I have another ticket a lot of issues with my pro sites not working when trying to create an account from the front end.
    You seem to know what you are doing so can you take a look at that for me?

  • Panos

    Hi Tyson,

    The blog_can_have_more_plugins() function is used to check if blog can allow more plugins to activate. You can see it in action inside the admin_notice() function where it checks like this:
    if( $this->blog_can_have_more_plugins() )

    To show a different message per level you need to check the level in the admin_notice(). Say the level you want to check has id 6:

    global $blog_id;
    if( $This->get_blog_level( $blog_id ) == 6 ){
    //show the message for that level
    }
    else{
    //the message for the other levels
    }

    I will search your other tickets to see how I could help

    Cheers!