2

When solving this issue ( Change admin language based on user (in single-site) ), I did a function so that the plugin would auto-hide from all users except one (me:).

The flaw is that it only auto-hides when active.

At first, it was a mu-plugin, but I had to enable/disable it manually. It really doesn't need to run all the time, as it may slow the site (?) or even contain some sensitive manipulation of the site's language (another question mark).

But mainly the motivation is that I don't want the client activating/deactivating/deleting this plugin (is for my own use), and don't want it cluttering her own plugins list.

So, decided to split the functions in two plugins:

  • One is a Must-Use used to hide the regular one.
  • The other will be activated/deactivated upon necessity and is the one from the above linked issue.

But now I run into another question: how to hide one plugin from the Must-Use listing?

3
  • Everytime I stumble upon this Q/A I want to +1 it another time.
    – kaiser
    Commented Sep 20, 2013 at 0:00
  • 1
    @kaiser, yep, I like this trick a lot too, No MustUse nor DontDisable here, folks, please, move along, move along... :) You know you can +100, right? :P
    – brasofilo
    Commented Sep 20, 2013 at 0:20
  • Yep. And I will. Just right after I passed Mike and can afford 100 :)
    – kaiser
    Commented Sep 20, 2013 at 2:12

2 Answers 2

4

Apparently, there's no way to remove one plugin from the Must-Use list...

But then, found a filter that does even better:
show_advanced_plugins will completely hide the sight of the Must-Use plugins.

After all, if you're trying to hide something of this nature, then hide it all...

It's inside the class WP_Plugins_List_Table and takes two arguments: a boolean and the plugin type (mustuse and dropins).

Check this article for a reference of MU and specially Drop-ins. But, for the sake of the wiki, I'll post the DI table at the end of this answer.


Update:
- mu-plugin updated to multiple users and multiple hide plugins was string now it's array


The Drop-in plugin

For testing this procedure we need one of those. Create a file named db.php at the root of your wp-content folder and copy this:

<?php
/*
    Plugin Name: Just for testing Drop-ins
    Version: 0.1
    Author: WPSE-53866
    Author URI: http://wordpress.stackexchange.com/
*/

The Must-use plugin

Then, this one inside the mu-plugins folder, and set the parameters of the class.

<?php
/*
Plugin Name: Hide Must-Use and Drop-ins from the Plugins listing
Plugin URI: http://wordpress.stackexchange.com/questions/53866
Description: Used to hide the Must-Use and Drop-ins plugins from all users except one. Also can hide a selected plugin from the regular list, be it active or not.
Version: 1.1
Author: brasofilo
AuthorURI: http://wordpress.stackexchange.com/users/12615/
*/

/**
 * Parameters of the class - sorry for the non-standard documentation
 *
 * @super_admin     : array   (required)    : the users which are able to see everything
 * @can_see_mustuse : boolean (required)    : can other users see the Must-Use list?
 * @can_see_dropins : boolean (required)    : can other users see the Drop-ins list?
 * @hide_this_one   : array   (optional)    : plugins to hide in the regular listing
 *
 * Reference article for Must-Use and Drop-ins
 * http://hakre.wordpress.com/2010/05/01/must-use-and-drop-ins-plugins/
 */

add_action('admin_init', 'wpse_53866_fire_plugin');

function wpse_53866_fire_plugin() 
{
    global $pagenow;
    if( 'plugins.php' != $pagenow) 
        return;

    $wpse53866_HideAdvancedPlugins_instance = new Wpse53866_HideAdvancedPlugins(
        array(
            'super_admin'       => array('Rodolfo','roda'),
            'can_see_mustuse'   => false,
            'can_see_dropins'   => true,
            'hide_this_ones'    => array( 'set-user-locale.php', 'akismet/akismet.php' )
        )
    );
}

class Wpse53866_HideAdvancedPlugins
{
    public function __construct($data)
    {       
        $this->user     = $data['super_admin'];
        $this->mustuse  = $data['can_see_mustuse'];
        $this->dropins  = $data['can_see_dropins'];
        $this->hide     = isset( $data['hide_this_ones'] ) ? $data['hide_this_ones'] : false;

        if($this->hide)
            add_filter( 'all_plugins', array(&$this, 'on_list_plugins' ) );

        add_filter( 'show_advanced_plugins', array(&$this, 'on_list_advanced' ), 10, 2 );
    }

    public function on_list_plugins($plugins)
    {
            global $current_user;

            if( ! in_array( $current_user->user_login, $this->user ) )
            {
                foreach( $this->hide as $plug )
                    unset( $plugins[$plug] );               
            }

           return $plugins;
    }

    public function on_list_advanced($show, $type)
    {
            global $current_user;

            if( ! in_array( $current_user->user_login, $this->user ) ) 
            {
                if( 'mustuse' == $type ) 
                    return $this->mustuse;

                if( 'dropins' == $type ) 
                    return $this->dropins;
            }

           return true;
    }
}

Reference

by hakre on wordpress

list of drop-ins plugins

5
  • 2
    Do not create an instance on every page load, wait until the hook fires you want to catch. Your plugin will now run even on AJAX requests. For language specific hacks use the $langname.php in /languages/.
    – fuxia
    Commented May 31, 2012 at 18:46
  • @toscho , oh, my, thanks maestro, looks like I did a brute force attack, will amend ASAP :)
    – brasofilo
    Commented May 31, 2012 at 18:55
  • Plugin initialization corrected for only firing in the right place. I simply copy-pasted another concept without attention to the consequences. Thanks toscho for the analytic eye. But the language hack is really very specific for enabling english as admin language for only one user on a german site, will revise its code as well JIC.
    – brasofilo
    Commented May 31, 2012 at 19:13
  • 1
    Put a de_DE.php into the language directory. It will be used like a plugin.
    – fuxia
    Commented May 31, 2012 at 19:16
  • @toscho - in this example, I'm not dealing with languages and am using akismet/akismet.php as the sample plugin to hide from the main list. In my text I'm referring to another plugin for swapping languages according to the user_login. It uses add_filter('locale', func); and manipulates the output. Thanks for the heads up!
    – brasofilo
    Commented May 31, 2012 at 19:38
1

Another use: in a Multisite environment, suppose that we have a site specific plugin. Not a Must Use, but a regular plugin, tailor made for just one site.

It lives on example.com/wp-content/plugins/my-plugin/my-plugin.php.

We'll make a Must Use plugin, /mu-plugins/hide-plugins.php like:

<?php
/**
 * Plugin Name: One site only
 * Description: Hide site specific plugin from other sites
 **/

add_action('plugins_loaded', function () 
{
    # Set filter to all sites, except ID 15
    $blog_id = get_current_blog_id();
    if( $blog_id == 15 )
        return;

    # Alternative for subdomain installs
    // if( 'example.com' !== $_SERVER['SERVER_NAME'] )

    add_filter( 'all_plugins', function( $plugins )
    {
        unset( $plugins['my-plugin/my-plugin.php'] ); 
        return $plugins;
    });
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.