WordPress AJAX login in sidebar without a plugin – Part I (Login)

In this AJAX login articles series we will implement best-practice techniques to provide Ajax login/logout mechanism for WordPress that will be available in sidebar.

UPDATE: I have created another article to do AJAX Login and Register in a popup box. You should give it a try.

AJAX login in sidebar without a plugin

By following this article you will be able to create a nice login box that resides in widget sidebar of your website and checks for user credentials and behaves accordingly.

AJAX login – HTML markup

First of all we need to create a login form in our page’s sidebar. To do this open your WordPress admin widgets page, drag a text widget to your desired sidebar, copy and paste the following code:

Notice at line number 3 in the above code, there is [xyz-ips snippet="ajaxLogin"] shortcode used. Actually we need to call ‘wp_nonce_field’ function which creates a hidden field with ID “security” and value “ajax-login-nonce” in hashed form. We must use and run php script in widget to achieve this.

You can use any plugin of your choice which creates shortcode to let you run the following php script in widget:

div #divuser at line number 10 will let us show display name of logged in user and a logout link after successful login.  Save the widget and check your page.

AJAX login text widget AJAX login form

You will find login form ready there and also inspect element or view it’s source that would be something like this:

Validating and sending user info via AJAX

When the user fills out the form and clicks LOGIN, client side validation is done to ensure that data is filled up in required format and sent to the server.

The script checks if the inserted data is correct and, based on the fact , notifies the user that the login was successful and performs the login and replaces the form with user display name and a logout link,  or just notifies him that his data is incorrect.

All of the server-side checking code will be placed inside of theme’s functions.php file. We’ll create a function that will include our JS files, create a new JS object called ajax_login_object and enable the not logged in users to call our function. So open your theme’s functions.php file and place the following code:

We have enqueued jquery.validate.js to validate our form. The ajax-login-script.js file performs AJAX login on form submit and change widget title to “Logged In” after successful login. Both files are available for download at end of this article and must be put inside js folder under your theme directory.

The wp_ajax_nopriv_ajaxlogin hook ensures that only users with no privileges (not logged in) can access the function ajax_login which we will add next in functions.php.

All that’s left is making the “LOGIN” button show our form and sending the form data with AJAX to the function above. To accomplish download the zip, unzip and place jquery.validate.js and ajax-login-script.js files inside js folder under your theme directory.

Download JS files

Styling

You can add style rules to your theme’s CSS file.  For example: I have used the following CSS rule to show error when validation fails:

That’s it! You have a working AJAX login form in sidebar now which you have created without a plugin. It performs login and loads #divuser with logged in user’s display name and link to AJAX logout.

Now we need to apply some functionality to show the same #divuserand keeping the login form hidden on page refresh or redirect for logged in user. In part II of this article we covered that as AJAX logout.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.