从用户隐藏(某些)WordPress插件的最简单方法?

时间:2010-09-14 08:14:33

标签: wordpress wordpress-plugin

我正在使用WordPress让我的用户创建自己的网站/博客。我有一个设置,我正在通过一些特殊的用户角色和标准插件克隆给所有用户。

但是,某些插件不应由用户更改或取消激活。

他们是否可以选择允许哪些插件使用不同的用户角色?或者一种简单的方法来隐藏插件页面中的一些插件,但仍然可以正常工作吗?

也许有一些插件可以帮助我做到这一点?

4 个答案:

答案 0 :(得分:7)

您可以编写一个插件,使用“all_plugins”过滤器挂钩从您不希望为特定用户显示的阵列插件中删除。像这样:

$plugin_credentials = array(
    'bob' => array(
            'Hello Dolly' => 1
    ),
    'jim' => array(
            'Akismet' => 1,
            'Hello Dolly' => 1,
    ),
    'admin' => "**ALL**"
);

function plugin_permissions($plugins)
{
        global $current_user, $plugin_credentials;

        $username = $current_user->user_login;

        if ($plugin_credentials[$username] == "**ALL**")
                return $plugins;

        $viewable_plugins = array();

        foreach ($plugins as $plugin) {
                if (isset($plugin_credentials[$username]) &&
                        isset($plugin_credentials[$username][$plugin['Name']]) &&
                        $plugin_credentials[$username][$plugin['Name']] == 1) {

                        array_push($viewable_plugins, $plugin);
                }
        }
        return $viewable_plugins;
}

add_filter('all_plugins', 'plugin_permissions');

管理插件本身的用户权限并不理想,但它可能最简单。您可以扩展这个想法,创建管理页面,以便在某个地方管理用户及其可查看的插件。

答案 1 :(得分:1)

每个插件通常会指定自己的角色/权限,如果您查看其add_submenu_page()或此类函数调用,您可以看到它们。您可以为这些插件创建新角色并替换作者指定的插件,但如果升级插件,它也会破坏更改。

答案 2 :(得分:0)

您应该对用户进行分层。确保管理员用户信任并且知道不要弄乱他们不理解的内容。其他人应该限于他们的角色。作者,编辑等。例如,如果他们只是撰写文章的网站的一部分,那么他们不需要看其余部分。让他们成为作者并完成它。

这是客户教育的一部分。如果它是一个较小的客户,角色分层较少,那么将它们作为两个账户。告诉他们“这是你管理网站的帐户,你很少会使用它。这是你大部分时间用来编写和编辑的帐户。你可以在这里完成所有日常工作很可能永远不需要管理员帐户“。你不会总是对这种方法有好运,但是花在你垃圾上的时间和精力更少,你不应该浪费时间。

答案 3 :(得分:0)

我已经基于@spuriousdata Answer做了一个新版本。这个使用插件 slugs (文件名减去扩展名)来构建限制列表。这种方式更容易,因为我们可以使用第一级unset$key数组。

代码本身的配置说明。

<?php
/**
 * Plugin Name: Limit Plugins by User
 * Plugin URI: http://stackoverflow.com/q/14340131/1287812
 * Description: Show selected plugins for specific users. 
 * Based on the code by spuriousdata, http://stackoverflow.com/a/3713985.
 * Author: brasofilo
 * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
 * Version: 1.0
 * License: GPLv2 or later
 */

add_filter( 'all_plugins', 'plugin_permissions_so_3707134' );

/**
 * Filter the list of plugins according to user_login
 *
 * Usage: configure the variable $plugin_credentials, which holds a list of users and their plugins.
 * To give full access, put a simple string "ALL"
 * To grant only for some plugins, create an array with the Plugin Slug, 
 *    which is the file name without extension (akismet.php, hello.php)
 *
 * @return array List of plugins
 */
function plugin_permissions_so_3707134( $plugins )
{
    // Config
    $plugin_credentials = array(
        'admin' => "ALL",
        'other-admin' => array(
            'akismet',
        ),
        'another-admin' => array(
            'akismet',
            'hello',
        ),
    );

    // Current user
    global $current_user;
    $username = $current_user->user_login;

    // Super admin, return everything
    if ( "ALL" == $plugin_credentials[ $username ] )
        return $plugins;

    // Filter the plugins of the user
    foreach ( $plugins as $key => $value ) 
    { 
        // Get the file name minus extension
        $plugin_slug = basename( $key, '.php' );

        // If not in the list of allowed plugins, remove from array
        if( !in_array( $plugin_slug, $plugin_credentials[ $username ] ) )
            unset( $plugins[ $key ] );
    }

    return $plugins;
}
相关问题