Building a Custom WordPress Login Form with Flat UI

WordPress is the most populous and one of the best content management system in the world. It powers over 60 percent of websites in the world.

By default, the Login form in WordPress is only visible when trying to access the administration dashboard. But you may prefer a custom login page that integrates visually with your website design instead of using the WordPress default login.

In this tutorial, I will teach you how to build a custom login form which can be embedded in a post or page using a shortcode and in a specific theme location using template tag.

The Flat UI form component — with a great aesthetic — will be used for the login form in this tutorial.

Below is a screenshot of the custom login form.

WordPress Login Form

Are you looking for WordPress Theme?

Get Startup Framework for WordPress now!

Custom WordPress Login Form Development

Without further fussing, let’s get started.

First, create a folder called “plugin directory” where all of the plugin files will be stored.

Include the plugin file header as follows:

<?php

/*
  Plugin Name: Designmodo Login Form
  Plugin URI: http://designmodo.com
  Description: Simple Login form plugin that just work
  Version: 1.0
  Author: Agbonghama Collins
  Author URI: http://tech4sky.com
 */

Remember we are using the form component in Flat UI and need to include it to our plugin folder. Download the Flat UI, extract the files and copy the bootstrap, css and font folder to our plugin folder.

The PHP function dlf_form() below contains the Login HTML form code.

 function dlf_form() {

?>

<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
	<div class="login-form">
		<div class="form-group">
			<input name="login_name" type="text" class="form-control login-field" value="" placeholder="Username" id="login-name" />
			<label class="login-field-icon fui-user" for="login-name"></label>
		</div>

		<div class="form-group">
			<input  name="login_password" type="password" class="form-control login-field" value="" placeholder="Password" id="login-pass" />
			<label class="login-field-icon fui-lock" for="login-pass"></label>
		</div>
		<input class="btn btn-primary btn-lg btn-block" type="submit"  name="dlf_submit" value="Log in" />
</form>
</div>
<?php
}

We need to authenticate the username and password that will be entered to the login form. The dlf_auth() below handles that.

function dlf_auth( $username, $password ) {
global $user;
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] =  $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ) {
echo $user->get_error_message();
}
if ( !is_wp_error($user) ) {
wp_redirect(home_url('wp-admin'));
}
}

When an invalid username or password is entered, an error message informing the user that data supplied is invalid will be displayed with a link to the Lost your password page, assuming the user forgot his or her password.

WordPress Login Form Error

The dlf_process() function below display the login form to WordPress front-end, check if the login form has been submitted and then call the dlf_auth() function to authenticate the username and password. If the login credentials are deemed valid, the user will be redirected to WordPress admin dashboard.

Below is the code for the dlf_process() function:

function dlf_process() {
if (isset($_POST['dlf_submit'])) {
	dlf_auth($_POST['login_name'], $_POST['login_password']);
}

dlf_form();
}

To give our plugin the awesome look powered by Flat UI, we need to append the Bootstrap and Flat UI css stylesheet to WordPress header using WP enqueue function as shown below.

function flat_ui_kit() {
wp_enqueue_style('bootstrap-css', plugins_url('bootstrap/css/bootstrap.css', __FILE__));
wp_enqueue_style('flat-ui-kit', plugins_url('css/flat-ui.css', __FILE__));

}

add_action('wp_enqueue_scripts', 'flat_ui_kit');

To be able to easily embed this custom login form to a WordPress page, it has to be done via shortcode. The codes below provide a [dm_login_form] shortcode which when inserted to a post or page, the login form will be embedded.

function dlf_shortcode() {
ob_start();
dlf_process();
return ob_get_clean();
}

add_shortcode('dm_login_form', 'dlf_shortcode');

Voila! we are completed the coding of our custom WordPress login form plugin.

Using the Login Form Plugin

First, get the plugin installed and activated in your WordPress blog.

To embed the custom login form in a post or page, use the shortcode [dm_login_form]. To include it a specific location in your theme, use the template tag below.

<?php echo do_shortcode('[dm_login_form]'); ?>

Summary

I know a lot of us would love to have a customized login form on a WordPress-powered website. This tutorial should help you achieve it.

Here is the link to the plugin file to use in your WordPress site; you can also study the code to learn how it works.

If you have any questions, let me know in the comments below.

Agbonghama Collins is a web developer by day and a freelance writer / blogger by night. When not wrangling with code, he is fond of sleeping on the couch and writing on his personal blog. Follow me on Google+ and Twitter

Newsletter

11 Comments
  1. Rob Broley Jul 7, 4:59 pm

    Such a great form, very simplistic in design and beautifully designed.

    Reply
    +1
  2. Nagarjuna Jul 7, 7:07 pm

    How to use this in the wp login page

    Reply
    +5
  3. jazeppi Jul 7, 9:25 pm

    So could we just add these files to the plugin folder that is already inside our wordpress site?

    Reply
    +2
    • Agbonghama Collins Jul 11, 1:15 am

      Save yourself the stress and download the plugin file. install and follow the instruction on how to use it.

      Reply
      +2
  4. Juan Aug 6, 12:09 pm

    Hi, i´m trying to use the plugging but it gives me an error;

    all works perfect, the login form shows ok, but when i log in, it refreshes the same page and before the login forms is this msg;

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/frescocollective.com/web/asesoria/wp-content/themes/bridge/full_width.php:27) in /usr/home/frescocollective.com/web/asesoria/wp-includes/pluggable.php on line 861

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/frescocollective.com/web/asesoria/wp-content/themes/bridge/full_width.php:27) in /usr/home/frescocollective.com/web/asesoria/wp-includes/pluggable.php on line 862

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/frescocollective.com/web/asesoria/wp-content/themes/bridge/full_width.php:27) in /usr/home/frescocollective.com/web/asesoria/wp-includes/pluggable.php on line 863

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/frescocollective.com/web/asesoria/wp-content/themes/bridge/full_width.php:27) in /usr/home/frescocollective.com/web/asesoria/wp-includes/pluggable.php on line 1121

    Could you help me?

    PD. THnks a lot for the pluggin, the design is perfect!

    Reply
    0
    • Agbonghama Collins Aug 6, 1:09 pm

      Hi, sorry for the error.
      To fix it, add the code below to the very top of the plugin file.

      ob_start();

      That should fix it.
      Thanks.

      Reply
      +1
  5. Juan Aug 6, 2:55 pm

    Works perfect!

    Reply
    0
  6. p33p Aug 11, 8:43 am

    Thanks, but one more thing i already activated the plug-in and where would i suppose to put this codes that’s supposed to change wplogin? sorry noob here..

    Reply
    0
  7. geoff Aug 16, 6:52 pm

    Thanks for sharing this! Exactly what i’ve been trying to figure out. One question. How could you make this style take over the normal login, even without the shortcode. Would love to have a version that even the admin would see.

    Reply
    0
    • Agbonghama Collins Aug 16, 7:56 pm

      Hi Geoff am working on a premium version of this plugin that also include a more advance login and registration builder with the ability to over-ride the default WordPress login system.

      Should be released in a couple of weeks time.

      If you need the feature, I can get it implemented on your site. Am up for hire at a nominal fee.
      Email: me@w3guy.com

      Reply
      0

Leave a Reply

*
* Minimum length: 20 characters