3

I found this plugin and it disable plugins for users who are logged in, how can I change it to make disable plugins for contributor user role only?

add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );

function disable_logged_in_plugin( $plugins ) {

    // The 'option_active_plugins' hook occurs before any user information get generated,
    // so we need to require this file early to be able to check for logged in status
    require (ABSPATH . WPINC . '/pluggable.php');

    // If we are logged in, and not inside the WP Admin area
    if ( is_user_logged_in() & !is_admin() ) {

        // Use the plugin folder and main file name here.
        // is used here as an example
           $plugins_not_needed = array ('embed-image-links/embed-image-links.php',
           'external-featured-image/main.php','wp-noexternallinks/wp-noexternallinks.php' );
            foreach ( $plugins_not_needed as $plugin ) {
                $key = array_search( $plugin, $plugins );
                if ( false !== $key ) {
                    unset( $plugins[ $key ] );
                }
            }
        }

        return $plugins;
    }
1
  • is_admin() doesn't test if you're an administrator, it tests if you're in the admin area, the same way is_home, is_archive. and is_single work. is_admin will return true for a subscriber looking at WP Admin, and false for a super admin on the frontend
    – Tom J Nowell
    Commented Nov 19, 2016 at 2:00

1 Answer 1

3

Try this one. I just changed only current_user_can('contributor') instead of is_user_logged_in().

add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );

function disable_logged_in_plugin( $plugins ) {

    // The 'option_active_plugins' hook occurs before any user information get generated,
    // so we need to require this file early to be able to check for logged in status
    require (ABSPATH . WPINC . '/pluggable.php');

    // If we are logged in, and NOT an admin...
    if ( current_user_can('contributor') & !is_admin() ) {

        // Use the plugin folder and main file name here.
        // is used here as an example
           $plugins_not_needed = array ('embed-image-links/embed-image-links.php',
           'external-featured-image/main.php','wp-noexternallinks/wp-noexternallinks.php' );
            foreach ( $plugins_not_needed as $plugin ) {
                $key = array_search( $plugin, $plugins );
                if ( false !== $key ) {
                    unset( $plugins[ $key ] );
                }
            }
        }

        return $plugins;
    }
3
  • Does a "role" work here? Contributes is a role not a capability right? Commented Nov 19, 2016 at 4:06
  • 1
    Never mind, it checks out ;) Commented Nov 19, 2016 at 4:07
  • Nice find! require (ABSPATH . WPINC . '/pluggable.php'); was all I needed :) Edit: seems like that was in the question already. Oh well upvotes for everyone!
    – Jules
    Commented Jul 23, 2020 at 23:05

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.