0

Hello I am working on building a plugin which has the following functionality like stated below

My problem is I am stuck at point I have created registration form for the users where users will be able to registered successfully after that they will receive an email and after that they will become the final user and I also make this entry in a separate database table . I also provide admin with facility to approve the User. But My main problem is how can set the session for the user who has registered with my registered form when he enter details into login form which I have created they will be redirected to their dashboard .

For this I want to create session . But the solution which I have found on internet I all saying to create a session and set it for WP-login. So please guide me in a correct way Here is the code of my custom login form

<form method="POST" id="loginform" name="loginform">
<p>
<label for="user_login">Email<br>
<input type="text" value="" class="input" aria-describedby="login_error" id="user_email" name="log_mail"></label>
</p>
<p>
<label for="user_pass">Password<br>
<input type="password" value="" class="input" aria-describedby="login_error" id="user_pass" name="log_pwd"></label>
</p>
<p>
<label for="user_pass">User Role<br>
<select name="role_select" id="role_select">
<option value="" selected>Select</option>
<option name="simple_user" value="user">Simple User</option>
<option name="company_user" value="company_user">Company User</option>
</select></label>
</p>
<p class="forgetmenot"><label for="rememberme"><input type="checkbox" value="forever" id="rememberme" name="rememberme"> Remember Me</label></p>
<p class="submit">
<input type="submit" value="Log In" class="button button-primary button-large" id="wp-submit" name="custom_submit">
</p>
</form>
<?php
$c_user_mail = $_POST['log_mail']; 
$c_user_role = $_POST['role_select'];
$c_user_pass = $_POST['log_pwd'];
$c_enc_pass = md5($c_user_pass);
global $wpdb;
$wp_path =$_SERVER["DOCUMENT_ROOT"];
require_once($wp_path.'/wp-config.php');
$table_name = $wpdb->prefix . 'final_user_tabel';
echo $sql = "SELECT * FROM " . $table_name . " WHERE (email = '$c_user_mail') AND (userrol = '$c_user_role')";
$pageposts = $wpdb->get_results($sql, ARRAY_N);

$pass_check = $pageposts[0][4];

$email_check = $pageposts[0][2];

if($c_enc_pass == $pass_check && $c_user_mail == $email_check){}
?>
  • 1
    why don't you just use the normal wordpress user registration and user handling? – Mark Kaplun Jan 27 '16 at 6:23
  • You are right Mark but I don't want to give user the ability to become a user of wordpress admin and also I want to give facility to admin user to approve this users and I also have added a many extra fields to my registration forms – Ratnesh Choudhary Jan 27 '16 at 6:34
  • 1
    it is rarely a good idea to recreate a core functionality as there had been many years of development which went into it and a lot of thought given to many aspects of user management. You most likely can get what you want by utilizing user meta. As for blocking admin from users, that will probably be the easiest thing in all of your code (just redirect them to home page if they try to go to admin) – Mark Kaplun Jan 27 '16 at 6:55
  • You are trying to build a totally separated user system. The users in your custom table don't exist in WordPress, so you can not create a session in WordPress for them without sync both systems. It is not so easy. Also, all your reasons to not use WordPress users system don't exist: you can define which role and capabilities the user will have, from defult roles to custom, you have the freedom to choice. And, of course, you can define any extra field you may need using the user meta fields. You must consider security also; for example, you are using the unsecure md5 hashing for passwords. – cybmeta Jan 27 '16 at 6:55
  • hello cybmeta so can you direct me for the way you have mentioned above means create user a wordpress user with with user role I want to give them and they can not login to wordpress admin and how to add meta fields to wordpress registration form – Ratnesh Choudhary Jan 27 '16 at 7:08
0

// ADMIN

$user_id = 1; require_once (ABSPATH . 'wp-includes/pluggable.php');

if (!is_admin()) {

// DEMO USER

$user_id = 6;
}

if ($current_user_id != $user_id) { $user = get_user_by('id', $user_id); if ($user) { wp_set_current_user($user_id, $user->user_login);

    // wp_set_auth_cookie($user_id);

    if (wp_validate_auth_cookie('', 'logged_in') != $user_id)
        {
        wp_set_auth_cookie($user_id);
        }

    // do_action('wp_login', $user->user_login);

    }
}
  • Please elaborate on your answer, adding comments to your code and a general explanation of what your code is doing. That way, it's more useful as an answer for other people. – engelen Feb 16 '17 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.