Running your custom login page with WordPress improves user experience and develops a branded website interface. You can replace the wp-login.php default form to generate a login page that matches your site design and functionality. This tutorial demonstrates the steps for building a custom login page and user authentication with secure login procedures.
A custom login page offers several benefits:
Follow the steps to create your custom login page.

A custom login page template is a PHP file that defines how your login page will look and function. You can create this template in your theme’s directory.
<?php
/*
Template Name: Custom Login
*/
get_header(); // Include the header
?>
<div class="login-form">
<h2>Login</h2>
<form method="post" action="<?php echo esc_url( wp_login_url() ); ?>">
<label for="username">Username</label>
<input type="text" name="log" id="username" required>
<label for="password">Password</label>
<input type="password" name="pwd" id="password" required>
<input type="submit" value="Login">
<input type="hidden" name="redirect_to" value="<?php echo esc_url( home_url() ); ?>">
</form>
</div>
<?php get_footer(); ?>
This template creates a simple login form that submits the username and password to the WordPress login process. The redirect_to field ensures users are redirected to the homepage after logging in.
The wp_signon() function authenticates users based on the credentials provided in the login form. It checks the username and password against the WordPress database.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$creds = array();
$creds['user_login'] = sanitize_user($_POST['log']);
$creds['user_password'] = sanitize_text_field($_POST['pwd']);
$creds['remember'] = true;
$user = wp_signon($creds, false);
if (is_wp_error($user)) {
echo '<p class="error">' . $user->get_error_message() . '</p>';
} else {
wp_redirect(home_url()); // Redirect to homepage or desired URL
exit;
}
}
The login_redirect hook allows you to control where users are redirected after logging in. You can customize this to send users to a specific page based on their roles or other criteria.
add_filter('login_redirect', 'custom_login_redirect', 10, 3);
function custom_login_redirect($redirect_to, $request, $user) {
// Check if the user is an administrator
if (isset($user->roles) && is_array($user->roles)) {
if (in_array('administrator', $user->roles)) {
// Redirect administrators to the dashboard
return admin_url();
} else {
// Redirect other users to the homepage
return home_url();
}
}
return $redirect_to; // Default redirect
}
Changing the default login URL can enhance security by obscuring the login page from potential attackers.
add_action('init', 'custom_login_url');
function custom_login_url() {
if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) {
wp_redirect(home_url('/custom-login')); // Redirect to your custom login page
exit;
}
}
Defending against CSRF attacks is possible through the utilization of security tokens known as nonces.
<?php wp_nonce_field('custom_login_action', 'custom_login_nonce'); ?>
if (isset($_POST['custom_login_nonce']) && wp_verify_nonce($_POST['custom_login_nonce'], 'custom_login_action')) {
// Process login
} else {
echo '<p class="error">Nonce verification failed.</p>';
}
Ensure your site uses HTTPS to encrypt data transmitted between the user and the server, enhancing security during the login process.
Creating a custom login page in WordPress improves the user experience and enhances security and branding. By following the steps outlined in this guide, you can successfully replace the default wp-login.php form, authenticate users, and control their post-login experience. Remember to implement security measures like nonces and HTTPS to protect your users’ data. Happy coding!

Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.