1

I am using switch user plugin of Mário Valney in my wordpress page. The plugin allows me to switch with different users account profile. But, I need only users with specific role to switch between and one link to switch back to logged user. For example, I am user A and B,C,D are with user roles as 'Subscriber'. So, I login with A and switching menu displays B,C,D on the list. Say, I switched to user B, again I need to switch back to A. How's that possible?

The code that displays specific user role in switching list

     /**
     * Render the HTML for Switcher
     */
    public function render_html() {
        if ( $this->check_can_render() ) {  
            global $wp;
            $users = get_users('role=subscriber' );?> 
            if ( ! empty( $users ) ) : ?>
                <div id="su-wrapper">
                    <span class="su-wrapper-toggle" title="<?php _e( 'Click to expand/contract', SWITCH_USER_TEXTDOMAIN ) ?>"></span>
                    <h1><?php _e( 'Switch User:', SWITCH_USER_TEXTDOMAIN ) ?></h1>
                    <hr>
                    <ul>
                    <?php
                    $current_user_id = get_current_user_id();

                    foreach ( $users as $user ) {
                    if ( $user->ID == $current_user_id ) {
                    echo '<li class="current-user" data-user-id="' . $user->ID . '" title="' . __( "You are logged as this user", SWITCH_USER_TEXTDOMAIN ) . '">' . $user->user_login . '</li>';
                                } else {
                    echo '<li class="js-su-user" data-user-id="' . $user->ID . '" title="' . __( "Click to login as this user", SWITCH_USER_TEXTDOMAIN ) . '">' . $user->user_login . '</li>';
                                }
                            }   
                        ?>
                    </ul>
                    <?php wp_nonce_field( SWITCH_USER_CHANGE_USER_NONCE, 'su-change-user-security' ); ?>
                </div>
            <?php //endif;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.