該項目使用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_login
和project_user_pass
得到的值,傳遞給WordPress登錄過程?
這下面是我到目前爲止,但是當我打的登錄按鈕,我得到 Invalid username
和Incorrect 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');