Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Jeff Farthing

    (@jfarthing84)

    Look through TML’s files to see how it handles determining the user’s role to get an idea how to accomplish this.

    Thread Starter bambicruz

    (@bambicruz)

    Can you please explain? I’ve just spent over 3 hours trying to figure this out but couldn’t find where / what code to do this… Any guidance would be more than appreciated!

    Thanks

    Plugin Author Jeff Farthing

    (@jfarthing84)

    function tml_redirect_login_by_role() {
    	if ( Theme_My_Login::is_tml_page( 'login' ) && is_user_logged_in() ) {
    		$user = wp_get_current_user();
    		$user_role = reset( $user->roles );
    
    		switch ( $user_role ) {
    			case 'administrator' :
    				$redirect_to = 'SOME URL';
    				break;
    			case 'editor' :
    				$redirect_to = 'SOME URL';
    				break;
    			default :
    				// Catch all
    				$redirect_to = 'SOME URL';
    		}
    
    		wp_redirect( $redirect_to );
    		exit;
    	}
    }
    add_action( 'template_redirect', 'tml_redirect_login_by_role' );
    Thread Starter bambicruz

    (@bambicruz)

    Thank you!!

    Thread Starter bambicruz

    (@bambicruz)

    Jeff,

    Instead of the solution you offered above, I’d like logged in users to go to the page set in the “Redirection” field within the plugin (under Log In) whenever they try to go to the login page.

    What’s the best way to do this?

    Thanks

    Plugin Author Jeff Farthing

    (@jfarthing84)

    function tml_redirect_logged_in() {
    	if ( Theme_My_Login::is_tml_page( 'login' ) && is_user_logged_in() ) {
    		$redirect_to = apply_filters( 'login_redirect', admin_url( 'profile.php' ), '', wp_get_current_user() );
    		wp_redirect( $redirect_to );
    		exit;
    	}
    }
    add_action( 'template_redirect', 'tml_redirect_logged_in' );
    Thread Starter bambicruz

    (@bambicruz)

    I added your code to the themes function.php but it didn’t work. It still keeps redirecting to profile page for all user types…

    Plugin Author Jeff Farthing

    (@jfarthing84)

    Try making it a higher priority:

    add_action( 'template_redirect', 'tml_redirect_logged_in', 8 );

    Thread Starter bambicruz

    (@bambicruz)

    Amazing, thank you!!!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Redirect logged in users’ is closed to new replies.