移除 WordPress 后台插件管理的“编辑”与“停用”功能

出于某些目的(如多用户博客防止一些小白乱搞),需要禁止(删除/移除)WordPress后台插件管理的 “编辑” 与 “停用” 功能,具体是WordPress 后台-插件-已安装插件 那里的每一个插件都会有的“编辑”与“停用”入口。下面提供一段代码实现这个功能

在主题的 functions.php 文件下加入以下代码:

add_filter( 'plugin_action_links', 'slt_lock_plugins', 10, 4 );
function slt_lock_plugins( $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(
 'slt-custom-fields/slt-custom-fields.php',
 'slt-file-select/slt-file-select.php',
 'slt-simple-events/slt-simple-events.php',
 'slt-widgets/slt-widgets.php'
 )))
 unset( $actions['deactivate'] );
 return $actions;
}

加了这段代码后,WordPress后台的所以插件“编辑”功能就会被禁止了。

对于第 7 行开始的代码是禁止停用插件的函数代码,用作者的话来说:

In the code below, the $plugin_file values being tested for are those from my own core set of plugins. The values are just the paths (relative to /wp-content/plugins/) of the main plugin PHP files. Adjust this array as appropriate.

大概的意思是数组如slt-custom-fields/slt-custom-fields.php 对应的是/wp-content/plugins/ 下的php 文件,如果想要移除某个插件的停用按钮,想要找到该插件的产生停用功能 php文件,然后在代码那里如上面那样陈列出来。意思大概都对的应该,虽然我英文不怎么好。结果亲自实践,大概也是那么回事。

展开评论