BuddyDev

Search

[Resolved] Code adjustment

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #37466

    Hello,

    1. How can I make the redirect of this code to take priority over the redirect of buddypress profile completion plugin.

     function buddydev_redirect_on_first_login( $redirect_to, $redirect_url_specified, $user ) {
     
        //check if we have a valid user?
        if ( is_wp_error( $user ) ) {
            return $redirect_to;
        }
     
        //check for user's last activity
        $last_activity =  bp_get_user_last_activity( $user->ID );
     
        if ( empty( $last_activity ) ) {
            //it is the first login
            //update redirect url
            //I am redirecting to user's profile here
            //you may change it to anything
            $redirect_to = bp_core_get_user_domain($user->ID );
        }
     
        return $redirect_to;
    }
     
    add_filter( 'login_redirect', 'buddydev_redirect_on_first_login', 110, 3 ); 

    2. I want the code to redirct to custom page (welcome) instead of user profile

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #37469

    Hello Tosin,

    Thank you for posting. Please give it try to the following code.

    
    /**
     * On first login
     *
     * @param string  $user_login User login.
     * @param WP_User $user       User object.
     *
     */
    function buddydev_redirect_on_first_login( $user_login, $user ) {
    
    	if ( ! function_exists( 'buddypress' ) ) {
    		return;
    	}
    
    	// check for user's last activity.
    	$last_activity = bp_get_user_last_activity( $user->ID );
    
    	if ( empty( $last_activity ) ) {
    // Redirect you custom page.
    		bp_core_redirect( bp_loggedin_user_domain() );
    	}
    }
    
    add_action( 'wp_login', 'buddydev_redirect_on_first_login', -1, 2 );
    
    

    Let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #37471

    The code worked perfectly.

    Just wondering would it also be possible to determine second login redirect is this possible

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #37472

    Hello Tosin,

    Thank you for the acknowledgment. Please modify the following section.

    
    if ( empty( $last_activity ) ) {
    // Redirect you custom page.
    		bp_core_redirect( bp_loggedin_user_domain() );
    	}
    
    

    with the following.

    
    
    if ( empty( $last_activity ) ) {
    		update_user_meta( $user->ID, '_buddydev_first_login', 1 );
    		// Redirect you custom page.
    		bp_core_redirect( bp_loggedin_user_domain() );
    	} elseif ( get_user_meta( $user->ID, '_buddydev_first_login', true ) ) {
    		delete_user_meta( $user->ID, '_buddydev_first_login' );
    		// Try second login here.
    		bp_core_redirect( bp_loggedin_user_domain() );
    	}
    
    

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #37476

    It worked perfectly thank you very much

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #37479

    Hello Tosin,

    Thank you for the acknowledgment. I am glad that I could help.

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: resolved