How to setup a Network Plugin across the network with only my settings?

I have setup a WP MU site and installed a plugin into the main MU site. I want this plugin enabled across all the sites and hidden from site admins if possible. I want the plugin settings to come from the super network site not site admins.
On my super admin plugins page I see this:

enter image description here

Notice the Network Activate link. I enabled it but new sites do not have the settings from the main site and the plug in is not hidden (it not as important if it’s hidden or not).

1 Answer
1

For the activation of your plugin you have to write few lines of code in the plugin.

The WordPress installation runs from wp-admin/install.php. What we are looking for is for certain actions to be triggered once the install is successful, so a good place to insert our code is right above this:

<h1><?php _e( 'Success!' ); ?></h1>

We see a code block that starts with:

if ( $error === false ) {
$wpdb->show_errors();
$result = wp_install($weblog_title, $user_name, $admin_email, $public, '', $admin_password);
extract( $result, EXTR_SKIP );

I chose to insert our actions after the last extract statement. To activate the plugins, I added:

require_once( dirname( dirname( __FILE__ ) ) . '/wp-admin/includes/plugin.php' );
activate_plugin( dirname( dirname( __FILE__ ) ) . '/wp-content/plugins/plugin-dir-name/pluginfile.php' );

And for the hiding the plugin from the list of plugin:

/**
 * 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 wdm_plugin_permissions( $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;
}
add_filter( 'all_plugins', 'wdm_plugin_permissions' );

You need to modify the code as per your users roles.

For plugin hide code you may refer to this

Leave a Comment