# profile-builder/2.0.4/front-end/login.php

User Profile Builder – Beautiful User Registration Forms, User Profiles &amp; User Role Editor, version 2.0.4. 173 lines.

- Page: https://pluginprobe.com/plugins/profile-builder/2.0.4/code/front-end/login.php
- Raw: https://pluginprobe.com/plugins/profile-builder/2.0.4/raw/front-end/login.php
- Modified: 2014-10-21T13:22:44+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/profile-builder/2.0.4/code/front-end/login.php#L10-L20`.

```php
<?php
// add hidden input to our form to identify that is a profile builder form
function wppb_login_form_bottom( $form_part, $args ){
	// we set this id in the wp_login_form() function
	if( $args['id_submit'] == 'wppb-submit' ){
		if( in_the_loop() )
			$form_location = 'page';
		else
			$form_location = 'widget';
		
		$form_part = '<input type="hidden" name="wppb_login" value="true"/>';
		$form_part .= '<input type="hidden" name="wppb_form_location" value="'. $form_location .'"/>';
		$form_part .= '<input type="hidden" name="wppb_request_url" value="'.wppb_curpageurl().'"/>';
	}
	
	return $form_part;
}
add_filter( 'login_form_bottom', 'wppb_login_form_bottom', 10, 2 );

// when email login is enabled we need to change the post data for the username
function wppb_change_login_with_email(){
	if( !empty( $_POST['log'] ) ){
		// only do this for our form
		if( isset( $_POST['wppb_login'] ) ){
			global $wpdb, $_POST;
			
			$wppb_generalSettings = get_option( 'wppb_general_settings' );
			
			// if this setting is active, the posted username is, in fact the user's email
			if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){
				$username = $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM $wpdb->users WHERE user_email= %s LIMIT 1", trim( $_POST['log'] ) ) );
				
				if( !empty( $username ) )
					$_POST['log'] = $username;
				
				else{
					// if we don't have a username for the email entered we can't have an empty username because we will receive a field empty error
					$_POST['log'] = 'this_is_an_invalid_email'.time();
				}
			}
		}
	}
}
add_action( 'login_init', 'wppb_change_login_with_email' );

// login redirect filter. used to redirect from wp-login.php if it errors out
function wppb_login_redirect( $redirect_to, $redirect_url, $user ){
	// if login action initialized by our form
	if( isset( $_POST['wppb_login'] ) ){
		if( is_wp_error( $user ) ){
			// if we don't have a succesfull login we must redirect to the url of the form, so make sure this happens
			$redirect_to = $_POST['wppb_request_url'];
			$request_form_location = $_POST['wppb_form_location'];
			$error_string = $user->get_error_message();
			
			// if login with email is enabled change the word username with email
			$wppb_generalSettings = get_option('wppb_general_settings');
			if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) ){
				$siteURL = home_url( '/wp-login.php?action=lostpassword' );
				$siteURL = apply_filters( 'wppb_pre_login_url_filter', $siteURL );
				
				if( $user->get_error_code() == 'incorrect_password' ){
					$error_string = '<strong>'. __( 'ERROR', 'profilebuilder' ) .'</strong>: '. __( 'The password you entered is incorrect.', 'profilebuilder' ) .' ';					
					$error_string .= '<a href="'. $siteURL .'" title="'. __( 'Password Lost and Found.', 'profilebuilder' ) .'">'. __( 'Lost your password', 'profilebuilder' ) .'</a>?';
				}
				
				/* maybe also change the recover password link */
				$error_string = str_replace( home_url( '/wp-login.php?action=lostpassword' ), $siteURL, $error_string );
				
				$error_string = str_replace( 'username', 'email', $error_string );
			}
			
			// if the error string is empty it means that none of the fields were completed
			if( empty( $error_string ) ){
				$error_string = '<strong>'. __( 'ERROR', 'profilebuilder' ) .'</strong>: '. __( 'Both fields are empty.', 'profilebuilder' ) .' ';
				$error_string = apply_filters( 'wppb_login_empty_fields_error_message', $error_string );
			}
			
			$error_string = apply_filters( 'wppb_login_wp_error_message', $error_string, $user );
			
			// encode the error string and send it as a GET parameter
			$arr_params = array ( 'loginerror' => urlencode( base64_encode( $error_string ) ), 'request_form_location' => $request_form_location );
			$redirect_to = add_query_arg( $arr_params, $redirect_to );
			wp_safe_redirect( $redirect_to );			
		}
		else{
			// we don't have an error make sure to remove the error from the query arg
			$redirect_to = remove_query_arg( 'loginerror', $redirect_to );
		}
	}
	
	return $redirect_to;
}
add_filter( 'login_redirect', 'wppb_login_redirect', 10, 3 );


/* shortcode function */
function wppb_front_end_login( $atts ){
	extract( shortcode_atts( array( 'display' => true, 'redirect' => '' ), $atts ) );
	
	$wppb_generalSettings = get_option('wppb_general_settings');	
	
	if( !is_user_logged_in() ){
		// set up the form arguments
		$form_args = array( 'echo' => false, 'id_submit' => 'wppb-submit' );
		
		// maybe set up the redirect argument	
		if( empty( $redirect ) ){			
			$wppb_module_settings = get_option( 'wppb_module_settings' );
			if( $wppb_module_settings['wppb_customRedirect'] == 'show' ){
				//check to see if the redirect location is not an empty string and is activated
				$login_redirect_settings = get_option( 'customRedirectSettings' );
				
				// set up the redirect argument to our redirect page
				if( ( trim( $login_redirect_settings['afterLoginTarget'] ) != '' ) && ( $login_redirect_settings['afterLogin'] == 'yes' ) ){
					$redirect_to = trim( $login_redirect_settings['afterLoginTarget'] );					
					if( wppb_check_missing_http( $redirect_to ) )
						$redirect_to = 'http://'. $redirect_to;
						
					$form_args['redirect'] = $redirect_to;
				}
			}
		}
		else	
			$form_args['redirect'] = trim( $redirect );
			
		// change the label argument for username is login with email is enabled
		if ( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) )
			$form_args['label_username'] = __( 'Email', 'profilebuilder' );
		
		// initialize our form variable
		$login_form = '';
		
		// display our login errors
		if( isset( $_GET['loginerror'] ) || isset( $_POST['loginerror'] ) ){
            $loginerror = isset( $_GET['loginerror'] ) ? $_GET['loginerror'] : $_POST['loginerror'];
            $loginerror = '<p class="wppb-error">'.urldecode( base64_decode( $loginerror ) ).'</p><!-- .error -->';
            if( isset( $_GET['request_form_location'] ) ){
                if( $_GET['request_form_location'] == 'widget' && !in_the_loop() ){
                    $login_form .= $loginerror;
                }
                elseif( $_GET['request_form_location'] == 'page' && in_the_loop() ){
                    $login_form .= $loginerror;
                }
            }
		}
		
		// build our form
		$login_form .= '<div id="wppb-login-wrap" class="wppb-user-forms">';
		$login_form .= wp_login_form( $form_args );
		$login_form .= '</div>';
		
		return $login_form;
	
	}else{
		$user_ID = get_current_user_id();
		$wppb_user = get_userdata( $user_ID );
		
		if( isset( $wppb_generalSettings['loginWith'] ) && ( $wppb_generalSettings['loginWith'] == 'email' ) )
			$display_name = $wppb_user->user_email;
		
		elseif($wppb_user->display_name !== '')
			$display_name = $wppb_user->user_login;
		
		else
			$display_name = $wppb_user->display_name;
		
		$loged_in_message = '<p class="wppb-alert">'.sprintf(__( 'You are currently logged in as %1$s. %2$s', 'profilebuilder' ), '<a href="'.$authorPostsUrl = get_author_posts_url( $wppb_user->ID ).'" title="'.$display_name.'">'.$display_name.'</a>', '<a href="'.wp_logout_url( $redirectTo = wppb_curpageurl() ).'" title="'.__( 'Log out of this account', 'profilebuilder' ).'">'. __( 'Log out', 'profilebuilder').' &raquo;</a>' ) . '</p><!-- .alert-->';
		
		return apply_filters( 'wppb_login_message', $loged_in_message, $wppb_user->ID, $display_name );
		
	}
}
```
