Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect logged in users from login page whiles logged in


  • Coach Afrane
    Participant

    @coach-afrane

    I have BP Redirect To Profile that redirects to profile when a user is successfully logged in.

    Now, i want to prevent the user from accessing the LOGIN page after whiles logged in. Like, if a user tries to go back, or types the link to the login page, the user gets redirected to their respective profile pages.

    I am currently running WordPress 4.8.2 and Buddypress 2.9.1.
    Site url: http://myfarm.agrihomegh.com/

    Any help on that?

    Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)

  • livingflame
    Participant

    @livingflame

    @coach-afrane

    Functions.php

    // Limit the Access To WordPress Dashboard. Only Admin 
    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
     if ( is_admin() && !current_user_can( 'administrator' ) && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
     wp_redirect( home_url() );
     exit;
     }
    }
    // Limit the Access To WordPress Dashboard. Only Admin and Editor 
    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
     if ( is_admin() && !current_user_can( 'administrator' ) && !current_user_can( 'editor' ) && !( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
     wp_redirect( home_url() );
     exit;
     }
    }
    // BP Redirect To Profile 
    function redirect_to_profile( $redirect_to_calculated, $redirect_url_specified, $user ) {
    if ( ! $user || is_wp_error( $user ) ) {
    		return $redirect_to_calculated;
    	}
    //If the redirect is not specified, assume it to be dashboard
    	if ( empty( $redirect_to_calculated ) ) {
    		$redirect_to_calculated = admin_url();
    	}
    // if the user is not site admin, redirect to his/her profile
    	if ( ! is_super_admin( $user->ID ) ) {
    		return bp_core_get_user_domain( $user->ID );
    	} else {
    		//if site admin or not logged in, do not do anything much
    		return $redirect_to_calculated;
    	}
    }
    add_filter( 'login_redirect', 'redirect_to_profile', 100, 3 );
    // Exclude Admins from Directories and BP Widgets 
    add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users' );
    
    function buddydev_exclude_users( $args ) {
    	//do not exclude in admin
    	if( is_admin() && ! defined( 'DOING_AJAX' ) ) {
    		return $args;
    	}
    	
    	$excluded = isset( $args['exclude'] )? $args['exclude'] : array();
    
    	if( !is_array( $excluded ) ) {
    		$excluded = explode(',', $excluded );
    	}
    	
    	$user_ids = array( 1 ); //user ids
    	
    	
    	$excluded = array_merge( $excluded, $user_ids );
    	
    	$args['exclude'] = $excluded;
    	
    	return $args;
    }

    Coach Afrane
    Participant

    @coach-afrane

    Sorry, it didn’t work for me.

    Maybe i need to explain myself better.

    I want any user who is already logged, to be redirected to the profile if the user tries to access the login page (which i have made the home page).

    I am using a multisite.

    I hope i didn’t have to apply all the snippets you provided. I used only the BP redirect to profile and it didn’t work.

    Thanks.


    livingflame
    Participant

    @livingflame

    Okey @coach-afrane, you want something like this: Thrive link


    Coach Afrane
    Participant

    @coach-afrane

    You just showed me some really nice idea for my community.

    However, it is not exactly what i need.

    I was able to go back to the login page when i click on the link you gave, though it showed “Great! You have successfully login.”

    In my case, after logging in, if you click on the homepage(which is the login page) link or logo, it will take you to your profile since you are logged in already.

    Thanks for your patience.


    livingflame
    Participant

    @livingflame

    I understand you now.

    You can try this: Link


    xxjonfenxx
    Participant

    @xxjonfenxx

    If it’s just a redirect to back to their profile page when they try to access the home page, you could add some code to the home page that looks something like this.

    
    if( is_user_logged_in() ) {
      $redirect_url = bp_loggedin_user_domain();
      wp_redirect( $redirect_url );
    }
    

    Coach Afrane
    Participant

    @coach-afrane

    Thanks a lot @xxjonfenxx.

    But, how do i add the code to the home page?

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar