How to create frontend login template in wordpress without plugin
frontend login in WordPress is a very appealing feature for user and site admin. In this tutorial, you can learn how to create a frontend login template in WordPress without a plugin.
Step 1: Create a custom template in your WordPress theme
Spet 2:
Add this following codes in the custom template
<?php if(!empty($errors)) { // to print errors, foreach($errors as $err ) echo $err; } ?>
<form name="loginform" action="https://yourwebsite.com/shop" method="post" class="clf-style"> <div class="login-logo"> <img src="#" alt="Logo" /> </div> <p class="login-username"> <input type="text" name="userName" id="user_login" class="input" value="" placeholder="Username" > </p> <p class="login-password"> <input type="password" name="passWord" id="user_pass" class="input" value="" placeholder="Password"> </p> <p class="login-submit"> <input type="hidden" name="login_Sbumit" > <input type="submit" name="wp-submit" id="wp-submit" class="”btn fa-input button-primary" value=""> <input type="hidden" name="redirect_to" value="http://localhost/wp/kvcodesplugin/"> </p> </form>
<?php if(isset($_POST['login_Sbumit'])) { $creds = array(); $creds['user_login'] = stripslashes( trim( $_POST['userName'] ) ); $creds['user_password'] = stripslashes( trim( $_POST['passWord'] ) ); $creds['remember'] = isset( $_POST['rememberMe'] ) ? sanitize_text_field( $_POST['rememberMe'] ) : ''; $redirect_to = esc_url_raw( $_POST['redirect_to'] ); $secure_cookie = null; if($redirect_to == '') $redirect_to= get_site_url(). '/dashboard/' ; if ( ! force_ssl_admin() ) { $user = is_email( $creds['user_login'] ) ? get_user_by( 'email', $creds['user_login'] ) : get_user_by( 'login', sanitize_user( $creds['user_login'] ) ); if ( $user && get_user_option( 'use_ssl', $user->ID ) ) { $secure_cookie = true; force_ssl_admin( true ); } } if ( force_ssl_admin() ) { $secure_cookie = true; } if ( is_null( $secure_cookie ) && force_ssl_login() ) { $secure_cookie = false; } $user = wp_signon( $creds, $secure_cookie ); if ( $secure_cookie && strstr( $redirect_to, 'wp-admin' ) ) { $redirect_to = str_replace( 'http:', 'https:', $redirect_to ); } if ( ! is_wp_error( $user ) ) { wp_safe_redirect( $redirect_to ); } else { if ( $user->errors ) { $errors['invalid_user'] = __('<strong>ERROR</strong>: Invalid user or password.'); } else { $errors['invalid_user_credentials'] = __( 'Please enter your username and password to login.', 'kvcodes' ); } } } ?>
Leave a Reply
You must be logged in to post a comment.