Enter your keyword

Blog

Custom Login Form in WordPress Frontend

// login form fields
function dsweb_login_form_fields() {
  ob_start(); 
    // show any error messages after form submission
     ?>
    <form id="loginform"  class="dsweb_form" action="" method="post">
      <fieldset>
        <p class="login-username">
          <label for="dsweb_user_Login">Username or Email</label>
          <input name="dsweb_user_login" id="dsweb_user_login" class="required" type="text"/>
        </p>
        <p class="login-password">
          <label for="dsweb_user_pass">Password</label>
          <input name="dsweb_user_pass" id="dsweb_user_pass" class="required" type="password"/>
        </p>
      <p class="login-remember"><label><input type="checkbox" value="forever" id="rememberme" name="rememberme"> Remember Me</label></p>
        <p class="login-submit">
          <input type="hidden" name="dsweb_login_nonce" value="<?php echo wp_create_nonce('dsweb-login-nonce'); ?>"/>
          <input class="button-primary" id="dsweb_login_submit" type="submit" value="Login"/>
        </p>
       <p class="lost-password-link">
        <a href="<?php echo esc_url( get_permalink(get_page_by_path('lost-password') ) ); ?>"><?php _e( 'Lost your password?', 'ib-educator' ); ?></a>
        </p>
      </fieldset>
    </form>
  <?php
  return ob_get_clean();
}
// logs a member in after submitting a form
function dsweb_login_member() {
  if(isset($_POST['dsweb_user_login']) && wp_verify_nonce($_POST['dsweb_login_nonce'], 'dsweb-login-nonce')) {
    // this returns the user ID and other info from the user name
  if ( is_email( $_POST['dsweb_user_login'] ) ) {
        $user = get_user_by_email( $_POST['dsweb_user_login'] );
        $username = $user->user_login;
    } else {
    $user = get_userdatabylogin($_POST['dsweb_user_login']);
    $username = $user->user_login;
  }

    if(!$user) {
      // if the user name doesn't exist
      dsweb_errors()->add('empty_username', __('Invalid username'));
    }
 
    if(!isset($_POST['dsweb_user_pass']) || $_POST['dsweb_user_pass'] == '') {
      // if no password was entered
      dsweb_errors()->add('empty_password', __('Please enter a password'));
    }
 
    // check the user's login with their password
    if(!wp_check_password($_POST['dsweb_user_pass'], $user->user_pass, $user->ID)) {
      // if the password is incorrect for the specified user
      dsweb_errors()->add('empty_password', __('Incorrect password'));
    }
 
    // retrieve all error messages
    $errors = dsweb_errors()->get_error_messages();
 
    // only log the user in if there are no errors
    if(empty($errors)) {	
      wp_setcookie($username, $_POST['dsweb_user_pass'], true);
      wp_set_current_user($user->ID, $username );
      do_action('wp_login', $username);
      wp_redirect(home_url().'/account/my-modules/'); exit;
    }
  }}
add_action('init', 'dsweb_login_member');
// used for tracking error messages
function dsweb_errors(){
    static $wp_error; // Will hold global variable safely
    return isset($wp_error) ? $wp_error : ($wp_error = new WP_Error(null, null, null));
}

// displays error messages from form submissions
function dsweb_show_error_messages() {
  if($codes = dsweb_errors()->get_error_codes()) {
    echo '<div class="dsweb_errors">';
        // Loop error codes and display errors
       foreach($codes as $code){
            $message = dsweb_errors()->get_error_message($code);
            echo '<span class="error"><strong>' . __('Error') . '</strong>: ' . $message . '</span><br/>';
        }
    echo ' </div>';
  }	
}

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.