Vraag:

In mijn vrije tijd beheer ik voor een aantal sportverenigingen of vrijwilliger organisaties WordPress websites. De websites worden door een of meerdere webmasters beheerd. Ik installeer alleen updates, soms nieuwe functionaliteiten en los de complexe problemen op.
Om het de webmasters zo gemakkelijk mogelijk te maken hebben zij de rol “beheerder” in WordPress ook wel “administrator” genoemd. Dit is de hoogste rol waardoor ze zelf zoveel mogelijk kunnen doen zonder mij hulp.
Maar deze rol biedt ook de mogelijkheid om WordPress plugins te bekijken, te installeren en in of uit te schakelen.
> Bekijk meer blogs over WordPress
Antwoord:
Om te voorkomen dat iedereen zomaar diverse plugins installeren in WordPress en daarmee nieuwe problemen veroorzaken heb ik deze optie uitgeschakeld. Dat heb ik gedaan middels het onderstaande PHP script. Ook het overzicht van plugins wordt verborgen.
Ik weet dat er ook plugins zijn waarmee je dit kan beheren. Maar onderstaande oplossing is lekker licht en vereist eigenlijk geen onderhoud.
Stap 1:
Maak in de map “/wp-content/” een map “mu-plugins” aan.
MU staat voor Must Use en betekent dat deze plugin altijd wordt ingeschakeld en ook niet verwijderd kan worden.
Een MU plugin is maximaal 1 PHP bestand. Je kan dus niet met mappen in deze map werken.
Stap 2:
Maak een bestand aan met een duidelijke naam, bijvoorbeeld domein-block-plugin-install.php
Stap 3:
Plaats onderstaande code in het PHP bestand uit stap 2.
Pas in de functie “unique_name_get_allowed_user_ids()” het ID aan naar het id van jouw gebruiker in de betreffende WordPress website. Deze id’s mogen nog wel plugins toevoegen / verwijderen / inschakelen / uitschakelen. Je kan hier ook eerdere ids aan toevoegen, het is immers een PHP array.
Elke functie heeft de prefix “unique_name_” om conflicten met andere plugins te voorkomen. Je kan deze hernoemen naar iets leukers, maar dat is niet verplicht.
Vergeet niet het bestand te uploaden naar de server.
<?php
/**
* Plugin Name: Your name
* Description: Restricts installing, updating, and deleting plugins and themes to a fixed list of admin user IDs.
* Version: 1.0.0
* Author: Timdehoog.nl
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
/**
* Get the list of user IDs that are allowed to install, update, or delete
* plugins and themes.
*
* @return int[] Array of allowed WordPress user IDs.
*/
function unique_name_get_allowed_user_ids(): array {
return [
1,
];
}
/**
* Get the list of capabilities that should be restricted.
*
* @return string[] Array of capability names.
*/
function unique_name_get_restricted_caps(): array {
return [
'install_plugins',
'update_plugins',
'delete_plugins',
'activate_plugins',
'install_themes',
'update_themes',
'delete_themes',
'edit_plugins',
'edit_themes',
];
}
/**
* Remove the restricted capabilities from any user who is not on the
* allow-list, even if they are an administrator.
*
* @param bool[] $allcaps Array of all the user's capabilities.
* @param string[] $caps Required primitive capabilities for the requested capability.
* @param array $args Arguments that accompany the requested capability check.
* @param WP_User $user The user object.
* @return bool[] Filtered array of the user's capabilities.
*/
function unique_name_restrict_install_caps($allcaps, $caps, $args, $user): array {
if (!$user || empty($user->ID)) {
return $allcaps;
}
// Allowed users: no changes, normal capabilities apply.
if (in_array((int) $user->ID, unique_name_get_allowed_user_ids(), true)) {
return $allcaps;
}
foreach (unique_name_get_restricted_caps() as $cap) {
if (isset($allcaps[$cap])) {
$allcaps[$cap] = false;
}
}
return $allcaps;
}
add_filter('user_has_cap', 'unique_name_restrict_install_caps', 10, 4);
/**
* Hide the plugin/theme install menu items for users who are not on the
* allow-list, so the UI does not present an entry point either.
*
* The capability check above is the actual protection; this only improves
* the admin UI.
*
* @return void
*/
function unique_name_hide_install_menus(): void {
$user_id = get_current_user_id();
if (in_array((int) $user_id, unique_name_get_allowed_user_ids(), true)) {
return;
}
remove_submenu_page('themes.php', 'theme-install.php');
remove_submenu_page('plugins.php', 'plugin-install.php');
}
add_action('admin_menu', 'unique_name_hide_install_menus', 999);
/**
* Block direct access to the plugin/theme install and editor pages for
* users who are not on the allow-list, in case they know the URL.
*
* @global string $pagenow The filename of the current admin screen.
*
* @return void
*/
function unique_name_block_direct_access(): void {
if (!is_admin()) {
return;
}
$user_id = get_current_user_id();
if (in_array((int) $user_id, unique_name_get_allowed_user_ids(), true)) {
return;
}
global $pagenow;
$blocked_pages = [
'plugin-install.php',
'theme-install.php',
'plugin-editor.php',
'theme-editor.php',
];
if (in_array($pagenow, $blocked_pages, true)) {
wp_die(esc_html__('You do not have permission to install or edit plugins or themes.', 'timdehoog-toolbox'));
}
}
add_action('admin_init', 'unique_name_block_direct_access');
Getest op:
WordPress 7.1.0 en hoger
