How to disable plugin management in WordPress for admin users

The problem

I manage WordPress sites for sports clubs and volunteer organizations. Their webmasters have the Administrator role, so they can work without my help. That role also lets them view, install, activate and deactivate plugins, which can cause new problems. How do can you disable plugin management?

The solution

A small must-use (MU) plugin blocks theme and plugin management for everyone except a fixed list of user IDs. It is lightweight and needs no maintenance. The unique_name_ prefix avoids conflicts with other plugins, and you can rename it.

A dedicated plugin could do the same, but this is simpler. MU plugins are always active and cannot be deactivated from the dashboard. Each must be a single PHP file, as subfolders are not loaded.

> Visit more Dutch blogs about WordPress

Setup

  1. Create a mu-plugins folder inside /wp-content/.
  2. Create a file such as block-plugin-install.php in that folder.
  3. Paste in the code below and set the allowed user IDs in unique_name_allowed_ids().
  4. Upload the file to the server.
  5. Theme and plugin management is now disabled.

The code

<?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, // Your own user id.
    ];
}

/**
 * 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-&gt;ID)) {
        return $allcaps;
    }

    // Allowed users: no changes, normal capabilities apply.
    if (in_array((int) $user-&gt;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');

Tested on

WordPress 7.1.0 and later.

Laat een reactie achter

Je e-mailadres zal niet worden gepubliceerd. vereiste velden zijn gemarkeerd met *

Deze site gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie gegevens worden verwerkt.