禁用特定用户角色的插件

时间:2016-11-19 作者:Cohagen

我找到了这个插件,它为登录的用户禁用了插件,如何才能将其更改为仅为贡献者用户角色禁用插件?

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 个回复
最合适的回答,由SO网友:Ranuka 整理而成

试试这个。我只是改变了current_user_can(\'contributor\') 而不是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;
    }