admin管理员组

文章数量:1122846

The following filter works great for preventing plugins from being deactivated...

add_filter( 'plugin_action_links', 'disable_plugin_deactivation', 10, 4 );

with...

// array_key_exists( 'edit', $actions )...
// array_key_exists( 'deactivate', $actions )...

...But is there a similar filter or array_key, that prevents plugins from displaying their uninstall link on the plugins.php file?

Thanks

The following filter works great for preventing plugins from being deactivated...

add_filter( 'plugin_action_links', 'disable_plugin_deactivation', 10, 4 );

with...

// array_key_exists( 'edit', $actions )...
// array_key_exists( 'deactivate', $actions )...

...But is there a similar filter or array_key, that prevents plugins from displaying their uninstall link on the plugins.php file?

Thanks

Share Improve this question edited Nov 9, 2017 at 23:40 klewis asked Nov 9, 2017 at 14:35 klewisklewis 8991 gold badge14 silver badges29 bronze badges 4
  • Are you talking about editors as in "people that just want to edit articles", or editors as in "users with the role editor"? The latter should not be able to delete plugins imho. – janh Commented Nov 9, 2017 at 14:40
  • Allow me to revise my original question....I overlooked my objective... – klewis Commented Nov 9, 2017 at 14:51
  • I don't think there's a direct way for that. You might be able to use register_deactivation_hook() to hook into the deactivation event and then just use activate_plugin() to immediately reactivate it. It's not the cleanest of ways, but should be pretty straightforward (if I'm not missing something major). – janh Commented Nov 9, 2017 at 15:35
  • please forgive me janh, allow me to revise my question once again... – klewis Commented Nov 9, 2017 at 21:27
Add a comment  | 

2 Answers 2

Reset to default 2

Yes, you can use the same filter, just remove the delete key of the $actions array. If you want to remove the "delete" link for the plugin "myplugin", you'd go for something like this:

add_filter("plugin_action_links", function($actions, $plugin_file, $plugin_data, $context) {
    if($plugin_file == "myplugin/myplugin.php") {
        unset($actions["delete"]);
    }
    return $actions;
}, 10, 4);

Obviously, you cannot put this into the plugin itself, since it will have been deactivated or the delete link will not show up (link to deactivate the plugin will be in its place). Also, be aware that this will only remove the link, it will not stop a determined user with the appropriate privileges from sending that request manually.

Nothing can stop deletion outside of WordPress, like direct removal of files from disk, so always have backups. However, it can be stopped from the Admin UI. Removing the links is nice but it's not enough, the user can just tick the plugin's checkbox, and Delete from the Bulk actions selector.

Adapt this to your needs and put it in a must-use plugin (e.g. wp-content/mu-plugins/stop-plugin-deletes.php), so it can't be deleted or deactivated, itself.

<?php

(function() {
    $required_plugin_file = function($plugin_file) {
        // don't have to use regular expressions
        // anything that returns a truthy value on matches will do, e.g. in_array()
        return preg_match(
            <<<'EOREGEXP'
/^(?:
    your-own-plugins-prefix-
    |a-plugin-you-need/
    |another-one/
    |a-basic-one\.php$
)/x
EOREGEXP
                ,
            $plugin_file
        );
    };
    // grind to a halt if deletion is attempted.
    // no other way as it's an action, not a filter.
    add_action(
        'delete_plugin',
        function($plugin_file) use ($required_plugin_file) {
            if ($required_plugin_file($plugin_file)) {
                wp_die(
                    'This plugin should not be deleted',
                    'Not Allowed',
                    403
                );
            }
            return $actions;
        },
        -1000000
    );
    // still remove links
    add_filter(
        'plugin_action_links',
        function($actions, $plugin_file, $plugin_data, $context) use ($required_plugin_file) {
            if ($required_plugin_file($plugin_file)) {
                unset($actions['delete']); // You may also want 'deactivate' here
            }
            return $actions;
        },
        1000000,
        4
    );
})();

本文标签: permissionsHow to prevent plugins from being uninstalled