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 |2 Answers
Reset to default 2Yes, 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
版权声明:本文标题:permissions - How to prevent plugins from being uninstalled 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736301231a1931107.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
register_deactivation_hook()
to hook into the deactivation event and then just useactivate_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