I need to allow only my admin user account access to 3 plugins

I would prefer that this was coded into the site rather than added as a plugin

'advanced-custom-fields',
'BackupBuddy',
'WPMU-DEV-Dashboard',

-Jade-

  • Vaughan

    Hi Jade,

    The dev dashboard is restricted to the admin that installed it. You can select the users by adding lines to wp-config.php see the docs.

    2. If you get locked out or need to enable a different user, or want to enable multiple users, you can simply put either of these constants in your wp-config.php file:

    define('WPMUDEV_LIMIT_TO_USER', '1'); – for one user.
    define('WPMUDEV_LIMIT_TO_USER', '1, 10'); – for multiple users (they are all user IDs).
    Place either of these just above the following line:
    /* That's all, stop editing! Happy blogging. */

    The other plugins, codewise i'm unsure. There are many plugins that are designed for that purpose to save you having to write complex code.

    Let me just ask 1 of our developers for some help with this.

    Thanks

  • Jade

    I was thinking of using something like this code

    <?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(
    'advanced-custom-fields',
    'BackupBuddy',
    'WPMU DEV Dashboard',
    ),

    );

    // 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;
    }

    but alter it so it does something like allow access to all plugins except

    'advanced-custom-fields',
    'BackupBuddy',
    'WPMU DEV Dashboard',

    apart from my admin user name which would be allowed to acess all plugins installed on the site

  • Vaughan

    Hi @jade,

    That might work.

    From the looks of the code how you have posted it.

    You would just need to change it slightly though.

    <?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 )
    {
    // plugins restricted
    $plugin_restrict = array('advanced-custom-fields',
    'backupbuddy',
    'wpmudev-updater');
    
    //usernames of each admin allowed access
    $allowed_users = array('admin');
    
    // Current user
    global $current_user;
    $username = $current_user->user_login;
    
    // Super admin, return everything
    if (in_array($username, $allowed_users)) 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_restrict ) )
    unset( $plugins[ $key ] );
    }
    
    return $plugins;
    }

    Hope this helps

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.