How to hide a plugin for non-admin users in wordpress

Today I will show how you can hide a plugin from the users that are non-admin. If you use a multisite, you can control a lots of stuffs with Pro Sites plugin. But what if you don’t want to use Pro Sites?

This simple scripts will do the job for you.

[php]

function custom_plugins( $plugins ){

if ( !is_super_admin() ) {
// Add comma separated paths of plugins that you want to hide
$hide_plugins = array(‘pluginFolder/pluginFile.php’, ‘pluginFile.php’);
foreach ($hide_plugins as $hide_plugin) {
unset($plugins[$hide_plugin]);
}
}
return $plugins;
}
add_filter(‘all_plugins’, ‘custom_plugins’);
[/php]

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name ‘mu-plugins’. If there is no folder in that name, then create a folder, name it ‘mu-plugins’, create a file inside that, give any name you like and paste the code in there. You don’t need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag <?php at the beginning of the code.

I have shown option for both – 1. if the plugin file is inside a folder 2. if the plugin file is in plugin root folder.

Just add more items in the array to hide :)

Leave a Reply

Your email address will not be published. Required fields are marked *