Created
November 10, 2020 20:57
-
-
Save murshed/3ade2ded4e38299895fc5ed26c6eda65 to your computer and use it in GitHub Desktop.
A simple wordpress plugin to login with only phone number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Phone Only Login | |
*/ | |
function generateRandomString($length = 10) | |
{ | |
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$charactersLength = strlen($characters); | |
$randomString = ''; | |
for ($i = 0;$i < $length;$i++) | |
{ | |
$randomString .= $characters[rand(0, $charactersLength - 1) ]; | |
} | |
return $randomString; | |
} | |
// Registration Form Shortcode | |
function registration_form_fn() | |
{ | |
if (is_user_logged_in()) | |
{ | |
echo 'User already logged in'; | |
} | |
else | |
{ | |
if ($_POST['email'] && $_POST['phone']) | |
{ | |
$email = htmlspecialchars($_POST['email']); | |
$phone = htmlspecialchars($_POST['phone']); | |
$password = generateRandomString(); | |
if (username_exists($phone) || email_exists($email)) | |
{ | |
echo 'Already Registered'; | |
} | |
else | |
{ | |
$userdata = array( | |
'user_login' => $phone, | |
'user_email' => $email, | |
'user_pass' => $password | |
); | |
$user_id = wp_insert_user($userdata); | |
if ($user_id) | |
{ | |
$updated = update_user_meta($user_id, 'saved_pword', $password); | |
echo 'User Registration Successfull'; | |
} | |
} | |
} | |
else | |
{ | |
?> | |
<form method="post"> | |
<div class="form-group"> | |
<label for="InputEmail">Email address</label> | |
<input name="email" type="email" class="form-control" id="InputEmail" placeholder="Enter email" required="required"> | |
</div> | |
<div class="form-group"> | |
<label for="InputPhone">Phone Number</label> | |
<input name="phone" type="text" class="form-control" id="InputPhone" placeholder="017....." required="required"> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<?php | |
} | |
} | |
} | |
add_shortcode('registration_form', 'registration_form_fn'); | |
// Login Form Shortcode | |
function login_form_fn() | |
{ | |
if (is_user_logged_in()) | |
{ | |
echo 'User already logged in'; | |
} | |
else | |
{ | |
?> | |
<form method="post"> | |
<div class="form-group"> | |
<label for="InputPhone">Phone Number</label> | |
<input name="ephone" type="text" class="form-control" id="InputPhone" placeholder="017....." required="required"> | |
</div> | |
<button type="submit" class="btn btn-primary">Login</button> | |
</form> | |
<?php | |
} | |
} | |
add_shortcode('login_form', 'login_form_fn'); | |
/** | |
* Perform automatic login. | |
*/ | |
function wpdocs_custom_login() | |
{ | |
if ($_POST['ephone']) | |
{ | |
$phone = htmlspecialchars($_POST['ephone']); | |
$user = get_user_by('login', $phone); | |
if ($user) | |
{ | |
$user_id = $user->ID; | |
$password = get_user_meta($user_id, 'saved_pword', true); | |
if ($password) | |
{ | |
$creds = array( | |
'user_login' => $phone, | |
'user_password' => $password, | |
'remember' => true | |
); | |
$user = wp_signon($creds, false); | |
if (is_wp_error($user)) | |
{ | |
echo $user->get_error_message(); | |
} | |
else | |
{ | |
wp_redirect(home_url()); | |
exit; | |
} | |
} | |
} else { | |
echo 'Wrong Phone Number'; | |
} | |
} | |
} | |
// Run before the headers and cookies are sent. | |
add_action('after_setup_theme', 'wpdocs_custom_login'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment