我制作了关于登录、注册和忘记密码的自定义插件。
我给我的插件添加操作template_redirect 钩子来验证电子邮件,当用户登录时,然后重定向到主页,并且忘记了密码。
但我有错误,我认为template\\u重定向挂钩不知道哪个是用于验证的重定向,哪个是用于忘记密码的重定向。
请查看我的代码:
这是自定义插件。php
<?php
if( ! defined(\'PLUGIN_PATH\') ){
define(\'PLUGIN_PATH\' , plugin_dir_path(__FILE__));
}
require_once ABSPATH . \'wp-admin/includes/plugin.php\';
require_once ABSPATH . \'wp-admin/includes/upgrade.php\';
require_once PLUGIN_PATH . \'includes/register.php\';
require_once PLUGIN_PATH . \'includes/login.php\';
require_once PLUGIN_PATH . \'includes/forgot-password.php\';
require_once PLUGIN_PATH . \'includes/change-password.php\';
require_once PLUGIN_PATH . \'includes/functions.php\';
if( ! class_exists(\'main\')){
class main {
function register(){
// Start session on init hook.
add_action( \'init\', array(\'myFunctions\',\'wpse16119876_init_session\') );
add_action (\'template_redirect\', array( \'forgotPasswordForm\', \'forgotPassword\'));
add_action (\'template_redirect\', array( \'loginForm\', \'verify\'));
add_action (\'template_redirect\', array( \'loginForm\', \'set_submit_login_func\'));
}
}
$main = new main();
$main->register();
}
?>
让我们假设我插入SMTP电子邮件设置并具有表单。我只给出有wp_重定向的函数。
然后这就是登录。php,
<?php
class loginForm extends registerForm{
public function set_submit_login_func(){
global $wpdb;
$account = filter_input(INPUT_POST, \'account\');
$password = filter_input(INPUT_POST, \'password\');
$users = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}users WHERE user_email = \'$account\' OR user_login = \'$account\'", ARRAY_A));
ob_start();
if(isset($_POST["login"])){
if($users){
if($users->user_status == 0){
if(wp_check_password($password, $users->user_pass)){
$credentials = array(
\'user_login\' => $account,
\'user_password\' => $password
);
wp_signon($credentials, true);
wp_redirect(site_url());
exit;
} else{
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">Password Salah</div>\';
}
} else{
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">Akun Belum di Aktifkan!</div>\';
}
}else{
if($account != ($users->user_email && $user_login)){
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">Akun Belum Terdaftar!</div>\';
}
}
}
session_destroy();
return ob_get_clean();
}
public function verify(){
global $wpdb;
$email = $_GET["em"];
$token = $_GET["tk"];
$url = site_url() .\'/login\';
$registered_date = date( \'Y-m-d H:i:s\', current_time( \'timestamp\', 0 ) );
$users = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}users WHERE user_email = \'$email\'", ARRAY_A));
if($users){
$user_token = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}user_token WHERE token = \'$token\'", ARRAY_A));
if($user_token){
if(time() - $user_token->date_created < (60*60*24)){
$wpdb->update($wpdb->prefix . \'users\', ["user_status" => 0, "user_registered" => $registered_date], ["user_email" => $email]);
$wpdb->delete($wpdb->prefix . \'user_token\', [\'email\' => $email]);
$_SESSION["message"] = \'<div style="background-color: darkcyan; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.$email.\' \'.\'telah aktif, silahkan login\'.\'</div>\';
wp_redirect($url);
exit;
}else{
$wpdb->delete($wpdb->prefix . \'users\', [\'user_email\' => $email]);
$wpdb->delete($wpdb->prefix . \'user_token\', [\'email\' => $email]);
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Aktivasi akun gagal! Token kadaluarsa\'.\'</div>\';
wp_redirect($url);
exit;
}
}else{
if(($token != $user_token) === true){
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Aktivasi akun gagal! Token salah\'.\'</div>\';
wp_redirect($url);
exit;
}
}
}else{
if(($email != $users->user_email) === true){
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Aktivasi akun gagal! Email salah\'.\'</div>\';
wp_redirect($url);
exit;
}
}
session_destroy();
}
}
?>
这是忘记密码。php
<?php
class forgotPasswordForm extends loginForm{
public function forgotPassword(){
global $wpdb;
$email = $_GET["em"];
$token = $_GET["tk"];
$url = site_url() .\'/login/forgotpassword\';
$other_url = add_query_arg(
array(
\'req:em\' =>$email,
\'tk\' => $token
), site_url().\'/changepassword\'
);
$users = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}users WHERE user_email = \'$email\'", ARRAY_A));
if($users){
$user_token = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}user_token WHERE token = \'$token\'", ARRAY_A));
if($user_token){
if(time() - $user_token->date_created < (60*60*24)){
$_SESSION["message"] = \'<div style="background-color: darkcyan ; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Setel ulang kata sandi: \'.$email.\'</div>\';
wp_redirect($other_url);
exit;
}
else{
$wpdb->delete($wpdb->prefix . \'user_token\', [\'email\' => $email]);
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Setel ulang kata sandi gagal! Token kadaluarsa\'.\'</div>\';
wp_redirect($url);
exit;
}
}
else{
if($token != $user_token){
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Setel ulang kata sandi gagal! Token salah\'.\'</div>\';
wp_redirect($url);
exit;
}
}
}else{
if($email != $users->user_email){
$_SESSION["message"] = \'<div style="background-color: indianred; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">\'.\'Setel ulang kata sandi gagal! Email salah\'.\'</div>\';
wp_redirect($url);
exit;
}
}
}
}
?>
我在函数中加入了sendmail函数。php
<?php
class myFunctions {
protected static function _sendEmail($token, $type){
$sendto = filter_input(INPUT_POST, \'email\');
$sendfrom = \'[email protected]\';
$headers = array("Content-type:text/html; charset=UTF-8","From: Me Myself <". $sendfrom . ">");
if($type == \'verify\'){
$sendsub = \'Verify Account\';
$sendmess = \'Please click to activate your account:<a href="\'.site_url(). \'/login/verify?em=\'.filter_input(INPUT_POST, \'email\'). \'&tk=\'.urlencode_deep($token).\'">Activate</a>\';
}
if($type == \'forgot\'){
$sendsub = \'Reset Password\';
$sendmess = \'Please click to reset your password:<a href="\'.site_url(). \'/login/forgotpassword?em=\'.filter_input(INPUT_POST, \'email\'). \'&tk=\'.urlencode_deep($token).\'">Reset Password</a>\';
}
wp_mail($sendto, $sendsub, $sendmess, $headers);
}
}
?>
对于继承,我的代码如下:
class myFunctions
class registerForm extends myFunctions
class loginForm extends registerForm
class forgotPasswordForm extends loginForm
class changePasswordForm extends forgotPasswordForm
这是\\u sendmail静态函数调用的地方:
这是里面忘记的密码。php
public function set_submit_forgot_password_func(){
global $wpdb;
$email = filter_input(INPUT_POST, \'email\', FILTER_SANITIZE_STRING);
$token = $this->token = base64_encode(random_bytes(32));
ob_start();
if(isset($_POST["forgot-password"])){
if(!filter_input(INPUT_POST, \'email\', FILTER_VALIDATE_EMAIL) === true){
$this->error["email"] = "* email tidak valid";
}
else if(!email_exists($email)){
$this->error["email"] = "* email tidak terdaftar";
}
else{
if($this->error["email"]){
unset($_POST);
return false;
}
else{
$_SESSION["message"] = \'<div style="background-color: darkcyan; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">Kami kirim email ke \'.$_POST["email"].\', mohon segera setel ulang kata sandi sebelum 24 jam dari sekarang!\'.\'</div>\';
$user_token = [
\'id\' => \'\',
\'email\' => $email,
\'token\' => $token,
\'date_created\' => time()
];
$wpdb->insert($wpdb->prefix . \'user_token\', $user_token);
forgotPasswordForm::_sendEmail($token, \'forgot\');
}
}
}
session_destroy();
return ob_get_clean();
}
这是登记簿。php代码发送电子邮件验证时:
public function set_submit_func(){
global $wpdb;
$fname = filter_input(INPUT_POST, \'fname\', FILTER_SANITIZE_STRING);
$lname = filter_input(INPUT_POST, \'lname\', FILTER_SANITIZE_STRING);
$username = filter_input(INPUT_POST, \'username\', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, \'email\', FILTER_SANITIZE_STRING);
$password = wp_hash_password(filter_input(INPUT_POST, \'password\', FILTER_DEFAULT));
$repeat_password = wp_hash_password(filter_input(INPUT_POST, \'repeat_password\', FILTER_DEFAULT));
$regexp_username = array("options"=>array("regexp"=>"/^[a-zA-Z\\d\\D]+$/"));
$regexp_name = array("options"=>array("regexp"=>"/^[a-zA-Z\\s]+$/"));
$regexp_password = array("options"=>array("regexp"=>"/^[a-zA-Z\\d\\D]+$/"));
$token = $this->token = base64_encode(random_bytes(32));
ob_start();
if(isset($_POST["submit2"])){
if(!filter_input(INPUT_POST, \'fname\', FILTER_VALIDATE_REGEXP, $regexp_name ) === true){
$this->error["fname"] = "* nama depan tidak valid";
}
if(!filter_input(INPUT_POST, \'lname\', FILTER_VALIDATE_REGEXP, $regexp_name) === true){
$this->error["lname"] = "* nama belakang tidak valid";
}
if(!filter_input(INPUT_POST, \'email\', FILTER_VALIDATE_EMAIL) === true){
$this->error["email"] = "* email tidak valid";
}
else if (email_exists($email)) {
$this->error["email"] = "* email sudah ter-registrasi";
}
if(!filter_input(INPUT_POST, \'username\', FILTER_VALIDATE_REGEXP, $regexp_username ) === true){
$this->error["username"] = "* username tidak valid";
}
else if (username_exists($username)){
$this->error["username"] = "* username telah terdaftar";
}
if(!filter_input(INPUT_POST, \'password\', FILTER_VALIDATE_REGEXP, $regexp_password) === true){
$this->error["password"] = "* password tidak valid";
}
else if(strlen($_POST["password"]) < 5){
$this->error["password"] = "* password tidak boleh kurang dari 5 karakter";
}
else if($_POST["repeat_password"] != $_POST["password"]){
$this->error["repeat_password"] = "* password tidak sama";
}
else{
if($this->error["lname"] || $this->error["fname"] || $this->error["username"] || $this->error["email"] || $this->error["password"] || $this->error["repeat_password"] ){
unset($_POST);
return false;
}
else{
$_SESSION["message"] = \'<div style="background-color: darkcyan; color: #FFFFFF; line-height: 30px; height: 10%; text-align: center; top: 0px; width: 100%; z-index: 100; margin-bottom: 10px;">Kami kirim email ke \'.$_POST["email"].\', mohon segera verifikasi sebelum 24 jam dari sekarang!</div>\';
$data = [
\'ID\' => \'\',
\'user_login\' => $username,
\'user_pass\' => $repeat_password,
\'user_nicename\' => $username,
\'user_email\' => $email,
\'user_status\' => \'1\',
\'display_name\' => $username
];
$user_token = [
\'id\' => \'\',
\'email\' => $email,
\'token\' => $token,
\'date_created\' => time()
];
$wpdb->insert($wpdb->prefix . \'users\', $data);
$wpdb->insert($wpdb->prefix . \'user_token\', $user_token);
registerForm::_sendEmail($token, \'verify\');
}
}
}
return ob_get_clean();
}
当sendEmail函数调用重置密码时:
但是,当我单击链接时,重定向到登录页面,而不是更改密码页面:
我希望我能得到帮助来解决这个bug。非常感谢。