自定义登录页面

Published
2022-12-08
浏览次数 :  20

创建页面,创建页面模板,进行跳转就可以

<?php
/**
 * Template Name:Custom Login
 */

global $user_ID;
//用来从数据库过滤数据
global $wpdb;
//也可以用is_user_logged_in()
//var_dump(is_user_logged_in());
//检查用户有没有登陆  
if (!$user_ID) {
  # user in logged out status 如果用户没登陆 
  # 也可以用 use is logged in 函数   
  if ($_POST) {
    # if the user clicked the log button send the post request 
    //等于 isset($_POST)

    $username = $wpdb->escape($_POST['username']);
    $password = $wpdb->escape($_POST['password']);

    $login_array = array();
    $login_array['user_login'] = $username;
    $login_array['user_password'] = $password;

    //check wp sign on  cookie函数  wpsignon 函数很重要
    $verify_user = wp_signon($login_array, true);

    //wp_signon 返回错误信息 , 登陆不成功 
    if (!is_wp_error($verify_user)) {
      # code...
      echo "<script>window.location = '" . site_url() . "'</script>";
    } else {

      echo "Invalid credentials!";
      
    }


  } else {
    get_header(); ?>

  <form method="post">
    <p>
      <label for="username">Username </label>
      <input type="text" id="username" name="username" placeholder="Enter Username/Email">
    </p>
    <p>
      <label for="password">Password </label>
      <input type="password" id="password" name="password" placeholder="Enter password">
    </p>
    <p><button type="submit" name="btn_submit">Log in</button></p>
  </form>


<?php get_footer();
  }
} else {
  //user is logged in 
  echo "你已经登陆";


}


  • 标签1
  • 标签1
  • 标签1
Top