Hi everyone, is there a way restrict access of a network-active plugin to the super-admin only? The plugin has to be network-active to function properly, but it is crucial that its settings are not accessible by others in the network, not even admins.
There should be a way of doing is with a custom function you could add to the functions.php file of your current theme.
// Function to restrict access to test-plugin settings
function restrict_test_plugin_settings() {
// Check if we are on the plugin's settings page
if (isset($_GET['page']) && $_GET['page'] === 'test-plugin-settings') {
// Check if current user is not a super-admin
if (!is_super_admin()) {
// Redirect non-super-admin users away from the settings page
wp_redirect(admin_url());
exit;
}
}
}
add_action('admin_init', 'restrict_test_plugin_settings');
Replace 'test-plugin-settings' with the actual slug of the settings page for the “test-plugin”.
@joachim-ratoff that worked, I logged in as a basic admin and was not able to open the plugin. Is there a way to hide the plugin from the list altogether?
I’m glad it worked! If what you want is to remove any trace of the plugin (both from the wp-admin menus and the plugins lists) for non super-admins you can try this one:
// Function to restrict access to test-plugin settings and hide it from non-super-admin users
function restrict_and_hide_test_plugin($plugins) {
// Check if current user is not a super-admin
if (!is_super_admin()) {
// Remove the plugin's menu item
remove_menu_page('test-plugin');
// Optionally, you can also hide the plugin's settings submenu
remove_submenu_page('test-plugin', 'test-plugin-settings');
// Check if the test-plugin is in the list of plugins
if (isset($plugins['test-plugin/test-plugin.php'])) {
// Remove the test-plugin from the list
unset($plugins['test-plugin/test-plugin.php']);
}
}
return $plugins;
}
add_action('admin_menu', 'restrict_and_hide_test_plugin');
add_filter('all_plugins', 'restrict_and_hide_test_plugin');
Again, remember to replace test-plugin with the right plugin slug.
That one didn’t work for me (I may not have replaced the correct slugs, not sure) but I did find a simpler solution with the Capabilities plugin. It let’s you edit or create user rules and specify what they have access to.