2016-01-13 57 views
1

該項目使用WordPress,由於WordPress登錄/註冊表單只是部分可定製的,我需要構建自己的HTML表單並將用戶名+密碼提交給WordPress。從HTML表單提交證書到WordPress登錄過程

我有一個問題,因爲我不知道如何從表單字段獲取數據並將它們提交給(通過)用於WordPress登錄的PHP處理器。

假設我有這樣的形式比較簡單的短代碼:

function project_login_form() { 

if(!is_user_logged_in()) { 

    $output = project_login_form_fields(); 
} 
return $output; 
} 
add_shortcode('login_form', 'project_login_form'); 

然後字段在該功能定義:

function project_login_form_fields() { 

ob_start(); 
    // show any error messages after form submission 
    project_show_error_messages(); ?> 
    <form id="project_login_form" class="col-md-8" action="" method="post"> 
     <div class="row"> 
      <div class="input-field col-md-3"> 
       <label for="project_user_login">Username</label> 
       <input name="project_user_login" id="project_user_login" class="required" type="text"/> 

      </div> 
      <div class="input-field col-md-3"> 
       <label for="project_user_pass">Password</label> 
       <input name="project_user_pass" id="project_user_pass" class="required" type="password"/> 
      </div> 
       <input type="hidden" name="project_login_nonce" value="<?php echo wp_create_nonce('project-login-nonce'); ?>"/> 
       <input id="project_login_submit" type="submit" value="Login"/> 

     <div> 
    </form> 
<?php 
return ob_get_clean(); 
} 

什麼是「連接」這一正確的方法所以要從 project_user_loginproject_user_pass得到的值,傳遞給WordPress登錄過程?

這下面是我到目前爲止,但是當我打的登錄按鈕,我得到 Invalid usernameIncorrect password (是的,他們是正確的,我相信:))

function project_login_member() { 

if(isset($_POST['project_user_login']) && wp_verify_nonce($_POST['project_login_nonce'], 'project-login-nonce')) { 

    // this returns the user ID and other info from the user name 
    $user = get_user_by('login','some-name'); 

    if(!$user) { 
     // if the user name doesn't exist 
     project_errors()->add('empty_username', __('Invalid username')); 
    } 

    if(!isset($_POST['project_user_pass']) || $_POST['project_user_pass'] == '') { 
     // if no password was entered 
     project_errors()->add('empty_password', __('Please enter a password')); 
    } 

    // check the user's login with their password 
    if(!wp_check_password($_POST['project_user_pass'], $user->user_pass, $user->ID)) { 
     // if the password is incorrect for the specified user 
     project_errors()->add('empty_password', __('Incorrect password')); 
    } 

    // retrieve all error messages 
    $errors = project_errors()->get_error_messages(); 

    // only log the user in if there are no errors 
    if(empty($errors)) { 

     wp_set_auth_cookie($_POST['project_user_login'], $_POST['project_user_pass'], true); 
     wp_set_current_user($user->ID, $_POST['project_user_login']); 
     do_action('wp_login', $_POST['project_user_login']); 

     wp_redirect(home_url()); exit; 
    } 
} 
} 
add_action('init', 'project_login_member'); 

回答

1

project_login_member()將成爲您正在偵聽POST特定請求的回調函數。你想鉤入add_action('wp', 'project_login_member');

接下來,你可以像這樣在你的表單中添加一個wp_nonce_field;

$registration_token = strtolower(wp_generate_password(10, false, false)) ?> 

<input type="hidden" name="registration_token" value="<?php echo  esc_attr($registration_token) ?>"> 

<?php wp_nonce_field('user_registration-' . $registration_token, 'registration_nonce') ?> 

<p> 
<button type="submit" id="submit-registration">Submit</button> 
</p> 

最後,設置回調監聽正確的事情,驗證您的隨機數場,然後添加其餘檢查你的回調。

function project_member_login() { 
    //Check to see if 'POST' exists 
    if ( 
     'POST' !== $_SERVER['REQUEST_METHOD'] 
     || 
     ! isset($_POST['registration_token']) 
     || 
     ! isset($_POST['registration_nonce']) 
     || 
     false === wp_verify_nonce($_POST['registration_nonce'], 'user_registration-' . $_POST['registration_token']) 

     ) { 
      return; 
     } 
     //sign user into WP 
     //save your $_POST vars into vars and use wp_signon() to log them in. 
     $creds = array(
      'user_login' => $user_login, 
      'user_password' => $user_pass, 
      'remember'  => true, 
     ); 

     $user = wp_signon($creds, false); 

     wp_redirect(home_url()); 
     exit; 
} 
0

您可以通過電子郵件或用戶名時,通過自定義表單提交

$user = get_user_by('email', '[email protected]'); 
$user = get_user_by('login', '[email protected]'); 

然後通過用戶ID和用戶登錄到該函數如下如下得到用戶對象:

if($user) { 
    wp_set_current_user($user->ID, $user->user_login); 
    wp_set_auth_cookie($user->ID); 
    do_action('wp_login', $user->user_login); 
} 

這將使您的用戶登錄發佈用戶名和密碼

注意WP_error,但成功登錄