Last active
October 8, 2022 05:30
-
-
Save mesbahulalam/200bf9b6fee3c66e36826f363bc81e62 to your computer and use it in GitHub Desktop.
Wordpress custom login and registration mechanism
This file contains 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 | |
add_filter( 'logout_url', 'change_logout_url', 10, 2 ); | |
function change_logout_url( $logout_url, $redirect ){ | |
$logout_url = site_url( '/logout/', 'login' ); | |
if ( ! empty( $redirect ) ) { | |
$logout_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $logout_url ); | |
} | |
return $login_url; | |
} | |
add_filter( 'login_url', 'change_login_url', 10, 2 ); | |
function change_login_url( $login_url, $redirect, $force_reauth ){ | |
$login_url = site_url( '/login/', 'login' ); | |
if ( ! empty( $redirect ) ) { | |
$login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url ); | |
} | |
if ( $force_reauth ) { | |
$login_url = add_query_arg( 'reauth', '1', $login_url ); | |
} | |
return $login_url; | |
} | |
add_filter( 'lostpassword_url', 'my_lost_password_page', 10, 2 ); | |
function my_lost_password_page( $lostpassword_url, $redirect ) { | |
return home_url( '/login/?action=reset&redirect_to=' . $redirect ); | |
} | |
add_filter( 'register_url', $register, 10, 2 ); | |
function change_registration_url($register){ | |
$register_url = site_url('/register'); | |
return $register_url; | |
} | |
add_filter( 'retrieve_password_message', 'email_modifier', 10, 2 ); | |
function email_mofifier($message, $key, $user_login, $user_data){ | |
return str_replace('wp-login.php?action=rp&', '/reset-password/?', $message); | |
} | |
add_filter( 'lostpassword_url', 'my_lost_password_page', 10, 2 ); | |
function my_lost_password_page( $lostpassword_url, $redirect ) { | |
return home_url( '/login/?action=reset&redirect_to=' . $redirect ); | |
} | |
function redirect_login_page() { | |
$login_page = home_url( '/login/' ); | |
$page_viewed = basename($_SERVER['REQUEST_URI']); | |
if( $page_viewed == "wp-login.php" && $_SERVER['REQUEST_METHOD'] == 'GET') { | |
wp_redirect($login_page); | |
exit; | |
} | |
} | |
add_action( 'init', 'redirect_login_page' ); |
This file contains 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 $u = $_SERVER['REQUEST_URI']; ?> | |
<a href="<?=wp_lostpassword_url($r)?>">Lost Password</a> | |
<a href="<?=wp_login_url($r)?>">Login</a> | |
<a href="<?=wp_registration_url($r)?>">Register</a> | |
<a href="<?=wp_logout_url($r)?>Logout</a> |
This file contains 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 | |
$current_user = wp_get_current_user(); | |
//do_action('user_redirect_if_logged_in'); | |
if(is_user_logged_in()){ | |
$url = site_url(get_query_var('redirect_to', (current_user_can('manage-options') ? '/wp-admin' : '/profile'))); | |
wp_redirect( $url ); | |
exit; | |
} | |
if( 'POST' == $_SERVER['REQUEST_METHOD'] ){ | |
if(empty($_POST['user'])) $error[] = 'Username is required.'; | |
if(empty($_POST['pass'])) $error[] = 'Password is required.'; | |
if(empty($error)){ | |
$creds = array( | |
'user_login' => $_POST['user'], | |
'user_password' => $_POST['pass'], | |
'remember' => $_POST['rememberme'] | |
); | |
$user = wp_signon( $creds, false ); | |
if ( is_wp_error( $user ) ) { | |
$error = $user->get_error_message(); | |
}else{ | |
$url = site_url(get_query_var('redirect_to', '/dashboard')); // user dashboard url | |
wp_redirect( $url ); | |
exit; | |
} | |
} | |
} |
This file contains 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 | |
wp_logout(); //erase all authentication traces from browser | |
wp_redirect(home_url()); // redirect or do other actions. | |
?> |
This file contains 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 | |
if($_SERVER['REQUEST_METHOD'] == "POST" AND isset($_POST['email'])){ | |
$r = retrieve_password($_POST['email']); | |
if(is_wp_error($r)){ | |
$error = $r->get_error_message(); | |
} | |
} |
This file contains 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 | |
$user_name = $_POST['user']; | |
$password = $_POST['pass']; | |
$user_email = $_POST['email']; | |
$user_id = username_exists( $user_name ); | |
if ( ! $user_id && false == email_exists( $user_email ) ) { | |
$user_id = wp_create_user( $user_name, $password, $user_email ); | |
//Send email for verification here | |
add_user_meta($user_id, 'evhash', 'random_hash'); | |
// wp_mail(); | |
echo 'Check your email for verification link'; | |
} else { | |
echo 'User already exists.'; | |
} |
This file contains 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 | |
$user = check_password_reset_key( $_GET['key'], $_GET['login'] ); | |
if ( ! $user || is_wp_error( $user ) ) { | |
if ( $user && $user->get_error_code() === 'expired_key' ) { | |
$error = 'Key expired.'; | |
} else { | |
$error = 'Invalid Key.'; | |
} | |
} | |
// if the url checks out, proceed to password reset form | |
if(isset($_POST['pass1']) AND isset($_POST['pass2']) and $_POST['pass1'] == $_POST['pass2']){ | |
reset_password( $user, $_POST['pass1'] ); | |
wp_redirect( site_url('/login/?password=changed') ); | |
exit; | |
} | |
if(empty($_POST['pass1']) OR empty($_POST['pass2'])){ | |
$error = 'Please fill both fields for your own security.'; | |
}elseif($_POST['pass1'] != $_POST['pass2']){ | |
$error = 'Ummmm.... They are two different passwords, which one should I save?'; | |
} |
This file contains 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 | |
if(isset($_GET['email']) AND isset($_GET['hash'])){ | |
$user = get_user_by('email', $_GET['email']); | |
if(is_wp_error($user)){ | |
$error = $user->get_error_message(); | |
}else{ | |
if($_GET['hash'] = get_user_meta($user->ID, 'evhash', true)){ | |
//verified; | |
delete_user_meta($user->ID, 'evhash'); | |
wp_redirect(home_url('/dashboard')); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment