This project uses WordPress, and since WordPress Login/Register Forms are only partially customizable, I need to build my own HTML Form and submit the UserName + Password to WordPress.
I have a problem as I don’t know how to get the Data from the Form Fields and submit them to (via) PHP processor for the WordPress Login.
Let’s assume I have this relatively simple Form as a ShortCode:
function project_login_form() {
if(!is_user_logged_in()) {
$output = project_login_form_fields();
}
return $output;
}
add_shortcode('login_form', 'project_login_form');
The Fields are then defined in this function:
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();
}
What would be the correct way to “Connect” this so to get the value from
project_user_login
and project_user_pass
, to pass it to the WordPress Login Process?
This below is what I have so far, but when I hit the Login Button, I get
Invalid username
and Incorrect password
(Yes, they are correct, I am sure 🙂 )
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');
The
project_login_member()
is going to be your callback function that is listening for a specific request of POST. You want to hook that intoadd_action( 'wp', 'project_login_member' );
Next, you can add a wp_nonce_field in your form like this;
Last, set the callback to listen for the right thing, verify your nonce field and then add the rest of the checks for your callback.
You can get user object by email or by username when submitted by custom form
then pass the user ID and user login to this function below as follows :
This will make your user login having posted username and password