WordPress.org

Ready to get started?Download WordPress

Forums

WP-Members
[resolved] First time login - redirect to password change (2 posts)

  1. andrewcartmill
    Member
    Posted 2 years ago #

    So I've been hitting my head against the wall trying to come up with a solution for this, but I can't seem to be able to get users who have just registered to be redirected to the password change page on their first login.

    I've tried this using another plugin (peter's redirect login) and his suggestions for setting up a filter in my functions.php file, but it always ignores the filter, and heads to the content without forcing the user to see the password change page.

    Anyone have any ideas for another plugin / another solution for accomplishing this with wp-members?

    Cheers

    http://wordpress.org/extend/plugins/wp-members/

  2. Chad Butler
    Member
    Plugin Author

    Posted 2 years ago #

    That shouldn't be too difficult. WP-Members has a login redirect filter that you can use.

    The tricky catch is that it filters the value of $redirect_to BEFORE the user is logged in. So you need to grab the username that they are logging in with, and get the user ID with get_userdatabylogin.

    Then, because the filter comes before successful login, you need to add a little validation to this at the beginning so that if the user puts in a valid username, but not a valid password, the meta indicating they have had a first login should not be set.

    Then, I checked for the presence of a meta to tell me if they've logged in before, and if not, set the meta and the redirect. Otherwise, it returns the unfiltered value of the original redirect url:

    add_filter( 'wpmem_login_redirect', 'my_login_redirect', 1 );
    function my_login_redirect( $redirect_to )
    {
    	// get the user data
    	$user = get_userdatabylogin( $_POST['log'] );
    
    	// check to see if it's a good user/pass combination
    	$pass = wp_check_password( $_POST['pwd'], $user->user_pass, $user->ID );
    	if( ! $pass ) {
    		// and if not, we are done here...
    		return $redirect_to;
    	}
    
    	// check for the presence of a flag, but make sure
    	$first_login = get_user_meta( $user->ID, 'first_login', true );
    
    	// if no flag, then set one, and redirect to the password change page
    	if( ! $first_login ) {
    
    		update_user_meta( $user->ID, 'first_login', 'true', '' );
    
    		// redirect to the members-area page
    		// with querystring for password change
    
    		$url = 'http://yoursite.com/members-area/?a=pwdchange';
    		return $url;
    
    	} else {
    
    		// otherwise, quit right here...
    		return $redirect_to;
    
    	}
    }

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags