I know that is not the number of plugins, it's the quality. But anyway, I want to limit the number of plugins that can be installed on a WordPress installation. Any ideas?
It doesn't matter if the limit applies to all admins including me.
The most performant way to do this is probably by hooking into map_meta_cap
to conditionally disallow the install_plugins
capability. There are no database calls involved with that.
Here's an (untested) example:
add_filter( 'map_meta_cap', 'wpse291964_limit_plugins', 10, 2 );
function wpse291964_limit_plugins( $caps, $cap ) {
if ( 'install_plugins' === $cap ) {
$plugins = get_plugins();
// Only allow 10 plugins to be installed.
if ( count( $plugins ) > 10 ) {
$caps[] = 'do_not_allow';
}
}
return $caps;
}
I recommend you to watch this video by John Blackbourn to learn more about map_meta_cap
and friends.
There are a few possible solutions you can investigate further to limit the possibility to install plugins.
WordPress will check if the current user can install plugins with current_user_can('install_plugins')
before rendering the Add New button. If you create a mu-plugin that will check for the installed number of plugins, you can remove the capability for admins and limit the possibility to install more plugins.
Check with an enqueued JS script the number of plugins and hide/remove the button. This is just cosmetic, if the user knows the route to the installer, plugins can still be installed.
Learn more about must use plugins
Here's how you can check the number of plugins installed, and add/remove the install_plugins capability for administrators depending on the maximum number of plugins you set. As @iantsch mentioned, it sounds like a must use plugin would be the best place for this. That would prevent other people from messing with the plugin from the dashboard, and it would always be on this way. Also it won't be included in the count of installed plugins in my code below. You could just add this to your themes functions.php file, but then the code is theme specific, not installation specific.
add_action( 'admin_init', 'yourprefix_limit_plugin_installations' );
function yourprefix_limit_plugin_installations() {
// get administrator role object
$admin_role = get_role( 'administrator' );
// check if administrator role object exists...
if( $admin_role !== null ) {
// ...get number of "installed" plugins(i.e. deactivated or activated) in your wp-content/plugins folder
$number_of_plugins_installed = count( get_plugins() );
// ...set number of installed plugins you want to have as a maximum
$maximum_number_of_plugins = 29; // take your pick, 29 was just how many I had in my testing environment
// ...if the number of plugins installed is equal to or more than $maximum_number_of_plugins integer...
if( $number_of_plugins_installed >= $maximum_number_of_plugins ) {
// ...remove install_plugins capability from all administrators
$admin_role->remove_cap( 'install_plugins' );
}
// ...else there are less plugins installed than set in $maximum_number_of_plugins...
else {
// ...so ensure all administrator users have the install_plugins capability(which is a default WordPress setting)
$admin_role->add_cap( 'install_plugins' );
}
}
}