下面是如何检查已安装的插件数量,以及根据您设置的最大插件数量为管理员添加/删除install\\u插件功能的方法。正如@iantsch所提到的,这听起来像是一个必须使用的插件将是最好的地方。这将防止其他人从仪表板上搞乱插件,而且它总是这样。此外,它不会包含在我下面代码中已安装插件的数量中。您可以将其添加到主题函数中。php文件,但代码是特定于主题的,而不是特定于安装的。
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\' );
}
}
}