Last active
April 10, 2024 19:23
-
-
Save thisbit/0a8e52d533c60c0a0a62a847e6ab31b9 to your computer and use it in GitHub Desktop.
Prevent removal, dissabling, enabling and installation of themes or plugins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Lock Admin Area | |
* Plugin URI: https://example.com/my-awesome-plugin | |
* Description: This plugin prevents plugin and theme management, it should be used in conjunction with preventing file editing from WP | |
* Version: 1.0.0 | |
* Author: Thisbit | |
* Author URI: https://example.com | |
* License: GPL v2 or later | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
* Text Domain: lock-admin | |
**/ | |
if ( ! defined( 'ABSPATH' ) ) die( 'Invalid request.' ); | |
if ( 'production' !== wp_get_environment_type()) return; | |
// Remove install capabilities from the Administrator and Super Administrator role | |
function thisbit_remove_caps_from_roles() { | |
$roles_to_modify = array('administrator', 'super_administrator'); | |
foreach ($roles_to_modify as $role_slug) { | |
$role = get_role($role_slug); | |
if ($role !== null) { | |
$role->remove_cap('install_themes'); | |
$role->remove_cap('install_plugins'); | |
$role->remove_cap('switch_themes'); | |
} | |
} | |
} | |
add_action('init', 'thisbit_remove_caps_from_roles'); | |
// Remove "Activate", "Deactivate" and "Delete" links for plugins in the admin area | |
function thisbit_remove_plugin_action_links($actions, $plugin_file, $plugin_data, $context) { | |
if (!defined('WP_CLI')) { | |
unset($actions['activate'], $actions['delete'], $actions['deactivate']); | |
} | |
return $actions; | |
} | |
add_filter('plugin_action_links', 'thisbit_remove_plugin_action_links', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This file HAS NOT BEEN TESTED ... it is purely an exploration as to how to lock down certain features from wp admin area. Use at your own peril :) no guarantees