Create WordPress ajax Login within 2 min

Step 1: Create Login form.

?php if (is_user_logged_in()) { ?>
<a class=”login_button” href=”<?php echo wp_logout_url( home_url() ); ?>”>Logout</a>
<?php } else { ?>

<form id=”login” action=”login” method=”post”>
<h1>Site Login</h1>
<p class=”status”></p>
<label for=”username”>Username</label>
<input id=”username” type=”text” name=”username”>
<label for=”password”>Password</label>
<input id=”password” type=”password” name=”password”>
<a class=”lost” href=”<?php echo wp_lostpassword_url(); ?>”>Lost your password?</a>
<input class=”submit_button” type=”submit” value=”Login” name=”submit”>
<?php wp_nonce_field( ‘ajax-login-nonce’, ‘security’ ); ?>
<?php } ?>

Step 2: Create ajax request.

Note: Paste this code in header.php or footer.php

jQuery(document).ready(function($) {

// Show the login dialog box on click
$(‘a#show_login’).on(‘click’, function(e){

$(‘div.login_overlay, form#login a.close’).on(‘click’, function(){

// Perform AJAX login on form submit
$(‘form#login’).on(‘submit’, function(e){
$(‘form#login p.status’).show().text(ajax_login_object.loadingmessage);
type: ‘POST’,
dataType: ‘json’,
url: ajax_login_object.ajaxurl,
data: {
‘action’: ‘ajaxlogin’, //calls wp_ajax_nopriv_ajaxlogin
‘username’: $(‘form#login #username’).val(),
‘password’: $(‘form#login #password’).val(),
‘security’: $(‘form#login #security’).val() },
success: function(data){
$(‘form#login p.status’).text(data.message);
if (data.loggedin == true){
document.location.href = ajax_login_object.redirecturl;


Step 3: Request data from database using function.

Note: Paste this code in function.php file

function ajax_login_init(){

wp_register_script(‘ajax-login-script’, get_template_directory_uri() . ‘/ajax-login-script.js’, array(‘jquery’) );

wp_localize_script( ‘ajax-login-script’, ‘ajax_login_object’, array(
‘ajaxurl’ => admin_url( ‘admin-ajax.php’ ),
‘redirecturl’ => get_permalink( get_the_ID()),
‘loadingmessage’ => __(‘Sending user info, please wait…’)

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( ‘wp_ajax_nopriv_ajaxlogin’, ‘ajax_login’ );

// Execute the action only if the user isn’t logged in
if (!is_user_logged_in()) {
add_action(‘init’, ‘ajax_login_init’);

function ajax_login(){

// First check the nonce, if it fails the function will break
check_ajax_referer( ‘ajax-login-nonce’, ‘security’ );

// Nonce is checked, get the POST data and sign user on
$info = array();
$info[‘user_login’] = $_POST[‘username’];
$info[‘user_password’] = $_POST[‘password’];
$info[‘remember’] = true;

$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array(‘loggedin’=>false, ‘message’=>__(‘Wrong username or password.’)));
} else {
echo json_encode(array(‘loggedin’=>true, ‘message’=>__(‘Login successful, redirecting…’)));


add_filter(‘show_admin_bar’, ‘__return_false’);





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s