• Resolved fried_eggz

    (@fried_eggz)


    Great plugin! We use it on a complex multisite and it works great. Keep up the good work.

    I have a support/feature request.

    I need to check if the current user is “switched to”. If one of our editors switch to a user I need to enable/disable certain features.

    I would like to do something like:

    if(user_is_switched) {
        // do some stuff
    }

    I tried porting the get_old_user() function from the plugin but it didnt work.

    Any ideas?

    https://wordpress.org/plugins/user-switching/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    Yep, there’s a current_user_switched() function just for this.

    Thread Starter fried_eggz

    (@fried_eggz)

    Thanks, found that in another support thread. Maybe put this in the FAQ?

    The problem is that calling that function in functions.php throws a

    fatal error: Call to undefined function is_user_logged_in()

    This is due to that it’s not safe to call is_user_logged_in() until init

    If using this function in functions.php you need something like this:

    function is_user_switched() {
    	if(current_user_switched()) {
    		// Do stuff
    	}
    }
    add_action('init', 'is_user_switched');

    I had to use it for a theme template file, that is my code:

    <?php if ( function_exists( 'current_user_switched' ) && current_user_switched() ) : ?>
    
    	<!-- switched-user -->
    
    <?php endif; ?>

    Oh, forgot to say again

    Thanks for the wonderful plugin John!!

    Exactly what I looked for. What I now need last is the ID of the user that had been switched to. Any Ideas for that?

    Thread Starter fried_eggz

    (@fried_eggz)

    @sixtyseven: get_current_user_id() should work.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Check if user is "switched"’ is closed to new replies.