Support » Plugin: Switch User » IDEA! Facebook styles account switching

  • Got a practical idea to enhance this plugin to allow users to switch between buddypress accounts (like you can do in facebook) –

    The allow multiple user accounts plugin https://wordpress.org/plugins/allow-multiple-accounts/ allows a user to register multiple user accounts with one email address…

    If we could filter the accounts listed in the switch user user list to only show the accounts the for email address of the logged in user, this could work pretty much like facebook allowing users to switch between their accounts…

    In the Allow Multiple Accounts plugin there already exists the functions needed to easily achieve this

    	/*
    	 * *******************
    	 * TEMPLATE FUNCTIONS
    	 *
    	 * Functions suitable for use in other themes and plugins
    	 * *******************
    	 */
    
    	/**
    	 * Returns a count of the number of users associated with a given email address.
    	 *
    	 * @since 2.0
    	 *
    	 * @param string $email The email address.
    	 * @return int   The number of users associated with the given email.
    	 */
    	if ( ! function_exists( 'c2c_count_multiple_accounts' ) ) {
    		function c2c_count_multiple_accounts( $email ) {
    			return c2c_AllowMultipleAccounts::get_instance()->count_multiple_accounts( $email );
    		}
    		add_action( 'c2c_count_multiple_accounts', 'c2c_count_multiple_accounts' );
    	}
    
    	/**
    	 * Returns the users associated with the given email address.
    	 *
    	 * @since 2.0
    	 *
    	 * @param string $email The email address.
    	 * @return array All of the users associated with the given email address.
    	 */
    	if ( ! function_exists( 'c2c_get_users_by_email' ) ) {
    		function c2c_get_users_by_email( $email ) {
    			return c2c_AllowMultipleAccounts::get_instance()->get_users_by_email( $email );
    		}
    		add_action( 'c2c_get_users_by_email', 'c2c_get_users_by_email' );
    	}
    
    	/**
    	 * Returns a boolean indicating if the given email address is associated with more
    	 * than one user account.
    	 *
    	 * @since 2.0
    	 *
    	 * @param string $email The email address.
    	 * @return bool  True if the email address is associated with more than one user account.
    	 */
    	if ( ! function_exists( 'c2c_has_multiple_accounts' ) ) {
    		function c2c_has_multiple_accounts( $email ) {
    			return c2c_AllowMultipleAccounts::get_instance()->has_multiple_accounts( $email );
    		}
    		add_action( 'c2c_has_multiple_accounts', 'c2c_has_multiple_accounts' );
    	}
Viewing 6 replies - 1 through 6 (of 6 total)
  • yeeeha…. got it working… if u would like this feel free to contact me here or at https://ourwordpress.guru/

    Plugin Author Mário Valney

    (@mariovalney)

    Hi Greg!

    Thanks for your post.

    I released a new version today (2.2) with a lot of new hooks.
    I hope it can help you.

    If you want to check the repository:
    https://github.com/mariovalney/switch-user

    @mariovalney awesome… getting it now… btw this is what i added to make it work with the multiple user accounts plugin https://wordpress.org/plugins/allow-multiple-accounts/

    in /wp-content/plugins/switch-user/modules/frontend/class-switch-user-frontend.php i replaced public function render_html() on line 156 with

    		public function render_html() {
    
    			if ( $this->check_can_render() ) {
    				
    				$users = get_users( array(
    					'order_by' => 'login',
    				) );
    
    				if ( ! empty( $users ) ) : ?>
    
    					<div id="su-wrapper">
    						<span class="su-wrapper-toggle">
    						<img src="https://gregs.life/wp-content/uploads/2018/12/switch_557552.png" style="width:32px;position: relative;top: -4px;" title="<?php _e( 'Switch Account', SWITCH_USER_TEXTDOMAIN ) ?>">
    						</span>
    						<h1><?php _e( 'Switch Account:', SWITCH_USER_TEXTDOMAIN ) ?></h1>
    						<hr>
    						<ul>
    							<?php
    								$current_user_id = get_current_user_id();
    								$current_user = wp_get_current_user();
    								$haslist = c2c_get_users_by_email( $current_user->user_email);
    								
    								foreach ( $haslist 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;
    			}
    
    		}
    Plugin Author Mário Valney

    (@mariovalney)

    Now you can use “su_front_get_users_args” filter to show only users you want (by ID, for example).

    Tried sending you a zip file of updated plugin to work with the the multiple user accounts plugin https://wordpress.org/plugins/allow-multiple-accounts/

    So basically this becomes an Account Switcher (as in facebook) allowing users to switch between accounts they have on your blog

    Plugin Author Mário Valney

    (@mariovalney)

    Hi Greg.

    I already added hooks to allow your plugin use mine as account switch.

    I’m not going to add your code to Switch User as it’s not a general purpose.

    Thanks,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘IDEA! Facebook styles account switching’ is closed to new replies.