我如何在wordpress中带来登录表单

时间:2012-11-30 16:27:22

标签: wordpress

我已尝试过here

中的代码

我甚至尝试过这段代码

   <?php wp_login_form(); ?>

但如果用户输入错误的用户名或密码,他们会被重定向到wp_login.php

是否有任何登录表单在页面中执行数据?

1 个答案:

答案 0 :(得分:1)

+1以获得明确的问题

您好,您可以使用此表格

 < ?php
 /*
Template Name: Custom WordPress Login
      */
  global $user_ID;

    if (!$user_ID) {

     if($_POST){
    //We shall SQL escape all inputs
      $username = $wpdb->escape($_REQUEST['username']);
     $password = $wpdb->escape($_REQUEST['password']);
    $remember = $wpdb->escape($_REQUEST['rememberme']);

    if($remember) $remember = "true";
       else $remember = "false";
  $login_data = array();
 $login_data['user_login'] = $username;
   $login_data['user_password'] = $password;
   $login_data['remember'] = $remember;
     $user_verify = wp_signon( $login_data, true ); 

    if ( is_wp_error($user_verify) ) 
     {
      echo "<span class="error">Invalid username or password. Please try again!</span>";
     exit();
   } else 
   {    
   echo "<script type="text/javascript">window.location='". get_bloginfo('url') ."'</script>";
   exit();
  }
} else { 

  get_header();

  ?>
      <div id="container">
      <div id="content">

     <!--?php the_title(); ?-->

     <div id="result"></div> <!-- To hold validation results -->
     <form id="wp_login_form" action="" method="post">

     <label>Username</label><br>
     <input type="text" name="username" class="text" value=""><br>
        <label>Password</label><br>
       <input type="password" name="password" class="text" value=""> <br>
    <label>
    <input name="rememberme" type="checkbox" value="forever">Remember me</label>
         <br><br>
     <input type="submit" id="submitbtn" name="submit" value="Login">

      </form>

       <script type="text/javascript">                          
       $("#submitbtn").click(function() {

          $('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif"               class="loader" />').fadeIn();
     var input_data = $('#wp_login_form').serialize();
     $.ajax({
     type: "POST",
      url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
     data: input_data,
      success: function(msg){
     $('.loader').remove();
    $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
    }
      });
      return false;

      });
    </script>

     </div>
      </div>
     <!--?php

    get_footer();
}
     }
    else {
echo "<script type='text/javascript'-->window.location='". get_bloginfo('url') ."'";
     }
      ?>