Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * logged in user.
- */
- add_action('template_redirect', 'make_user_login');
- function make_user_login(){
- if(isset($_GET['do']) && $_GET['do'] == 'login'):
- $errors = array();
- if(empty($_POST['usr']))
- $errors[] = 'Please enter a username.<br>';
- if(empty($_POST['upass']))
- $errors[] = 'Please enter a password.<br>';
- // sanitizing user input.
- $user_login = esc_attr($_POST['usr']);
- $user_pass = esc_attr($_POST['upass']);
- $sanitized_user_login = sanitize_user($user_login);
- if(empty($errors)):
- // checking user exists.
- if(username_exists($sanitized_user_login)) {
- $user = get_user_by( 'login', $sanitized_user_login);
- // getting user role
- $userRole = (!empty($user->roles) ? $user->roles : '');
- // checking user is subscriber.
- if(@in_array('subscriber', $userRole)) {
- // checking password is correct
- if (wp_check_password($user_pass, $user->user_pass, $user->ID)) {
- // Getting user data by user id.
- $user_data = get_userdata ($user->ID);
- if ($user_data !== false) {
- // setting up session for user.
- wp_clear_auth_cookie ();
- wp_set_auth_cookie ($user_data->ID, true);
- do_action ('wp_login', $user_data->user_login, $user_data);
- // Redirect user to home.
- wp_redirect (site_url());
- exit();
- }
- }
- else {
- $errors[] = 'Please enter correct password.<br>';
- }
- }
- else {
- $errors[] = 'You are not autorized to login.<br>';
- }
- }
- else {
- $errors[] = 'Please enter correct username.<br>';
- }
- endif;
- if(!empty($errors))
- define('LOGIN_ERROR', serialize($errors));
- endif;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement