How to Disable Plugin Deactivation from WordPress Admin Panel

0
0



One of the best parts about WordPress is the availability of plugins that makes your job easy as a user and as a developer. Often when creating sites for clients, we as developers use plugins that are absolutely crucial for the site to have certain functionalities. Recently, we ran into a problem where one of our client’s site was broken. The sidebar wasn’t loading completely, there were missing elements on the single post page which freaked them out. The bottom line of this story was that one of their new staff members who was new to WordPress deactivated some of the plugins which were required for the site to function properly. We went in and activated all the plugins that were deactivated, but there had to be something done in order for us to prevent this issue in the future. In this article, we will show you how to disable plugin deactivation from WordPress Admin Panel (only for specific plugins).

Theoretically, you should be able to use Justin Tadlock’s Members plugin and create new role for users. However, the client we had did not want to go this route. It is a small business, and they hired this new employee to deal with their social media and blog, so we cannot restrict access. They wanted to give him the ability to activate/deactivate plugins at his will. We had to find a way which would keep our client happy, and we also had to find a way to prevent this issue from happening in the future. Thankfully to Steve Taylor, we found a snippet that lets you remove the “Deactivate” link from specified plugins. It also removes the Edit link for all plugins because we did not want our client to edit any plugins through the editor.

So all you have to do is paste the following codes in your theme’s functions.php file:

add_filter( ‘plugin_action_links’, ‘disable_plugin_deactivation’, 10, 4 );
function disable_plugin_deactivation( $actions, $plugin_file, $plugin_data, $context ) {
// Remove edit link for all
if ( array_key_exists( ‘edit’, $actions ) )
unset( $actions[‘edit’] );
// Remove deactivate link for crucial plugins
if ( array_key_exists( ‘deactivate’, $actions ) && in_array( $plugin_file, array(
‘facebook-open-graph-meta-in-wordpress/fbogmeta.php’,
‘wp-pagenavi/wp-pagenavi.php’
)))
unset( $actions[‘deactivate’] );
return $actions;
}

Now, you need to change the array of $plugin_file where you see the list of specified plugins. The path of the file is relative to /wp-content/plugins/. So in the example above ‘facebook-open-graph-meta-in-wordpress/fbogmeta.php’ is a file located in the folder facebook-open-graph-meta-in-wordpress which is located inside the plugins folder. You can change the list to add as many plugins as you want.

This trick is a shortcut, and it does not actually prevent the actual deactivation. All we are doing is hiding the Deactivate link. Anyone with a little bit of WordPress knowledge can generate a deactivation URL and run it. But if your client is crafty enough to do that, then they already know what FTP is, and they can simply delete the plugins that way.

Are you working on a theme that absolutely requires a specific plugin for it to function properly? Then don’t forget to drop the code above.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>