Friday, December 17, 2010

Custom Login/Register/Password Code

In this DiW post, we transform three slices of code into a clean & stylish tabbed menu that visitors can use to login, register, and recover passwords from anywhere in your site. Too many features & details to explain up front, so check out the working demo to see the finished product. On the menu:

Default WordPress Login Page

Out of the box, WordPress uses wp-login.php for logging into the Admin, retrieving lost passwords, and registering for site membership. Each of these activities are handled on the same page, commonly referred to as the WordPress Login Page.

[ Screenshot: Default WordPress Login/Register Page ]
Yep, it’s the WordPress Login Page

As seen here, the Login Page shows the log-in form by default. Beneath the form are two links, “Register” and “Lost your password?”, which enable users to (yep, you guessed it) register and recover their password.

The cool thing about the Login Page is that the page doesn’t reload when either of these links are clicked. Instead, the form instantly changes from login to register or password to whatever. All three forms are included on the wp-login.php page and hidden/revealed as-needed using JavaScript. This “same-page” functionality is key to setting up our own custom login/register/password form elsewhere in our theme.

Moving the login/register/password form

While it’s not a good idea to move the entire wp-login.php file, it is possible to display the login/register/password forms anywhere in your theme. For example, putting the forms in your sidebar.php make it super-easy for visitors to register and login from anywhere in your site (here is an example). You could even create a WordPress page for each event: login, registration, and password-recovery pages that are customized/optimized in some really unique, bad-ass way.

The key to mobilizing the login forms is ensuring that they’ll work properly regardless of placement (before, after, or within the loop) in your theme template files. We also want to ensure that normal visitors who aren’t logged in see the forms, but logged-in users do not (or see alternate content).

Basically, it should work exactly like the default WordPress login functionality, but with one exception: instead of redirecting to the Admin Area (for log-ins) or to the Login Page (for registrations/password-recovery), we want the user to remain on the same page. This enables your guests to log-in, register, and reset passwords without leaving whatever page they happen to be visiting. Here’s the code to make it happen..

Custom WordPress template code

Here is the code to display the login/register/password form anywhere in your theme:

view as plain text

<div id="login-register-password">  <?php global $user_ID, $user_identity; get_currentuserinfo(); if (!$user_ID) { ?>  <ul class="tabs_login"> <li class="active_login">Login</li> <li>Register</li> <li>Forgot?</li> </ul> <div class="tab_container_login"> <div id="tab1_login" class="tab_content_login">  <?php $register = $_GET['register']; $reset = $_GET['reset']; if ($register == true) { ?>  <h3>Success!</h3> <p>Check your email for the password and then return to log in.</p>  <?php } elseif ($reset == true) { ?>  <h3>Success!</h3> <p>Check your email to reset your password.</p>  <?php } else { ?>  <h3>Have an account?</h3> <p>Log in or sign up! It&rsquo;s fast &amp; <em>free!</em></p>  <?php } ?>  <form method="post" action="<?php bloginfo('url') ?>/wp-login.php" class="wp-user-form"> <div class="username"> <label for="user_login"><?php _e('Username'); ?>: </label> <input type="text" name="log" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="11" /> </div> <div class="password"> <label for="user_pass"><?php _e('Password'); ?>: </label> <input type="password" name="pwd" value="" size="20" id="user_pass" tabindex="12" /> </div> <div class="login_fields"> <div class="rememberme"> <label for="rememberme"> <input type="checkbox" name="rememberme" value="forever" checked="checked" id="rememberme" tabindex="13" /> Remember me </label> </div> <?php do_action('login_form'); ?> <input type="submit" name="user-submit" value="<?php _e('Login'); ?>" tabindex="14" class="user-submit" /> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form> </div> <div id="tab2_login" class="tab_content_login" style="display:none;"> <h3>Register for this site!</h3> <p>Sign up now for the good stuff.</p> <form method="post" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" class="wp-user-form"> <div class="username"> <label for="user_login"><?php _e('Username'); ?>: </label> <input type="text" name="user_login" value="<?php echo esc_attr(stripslashes($user_login)); ?>" size="20" id="user_login" tabindex="101" /> </div> <div class="password"> <label for="user_email"><?php _e('Your Email'); ?>: </label> <input type="text" name="user_email" value="<?php echo esc_attr(stripslashes($user_email)); ?>" size="25" id="user_email" tabindex="102" /> </div> <div class="login_fields"> <?php do_action('register_form'); ?> <input type="submit" name="user-submit" value="<?php _e('Sign up!'); ?>" class="user-submit" tabindex="103" /> <?php $register = $_GET['register']; if($register == true) { echo '<p>Check your email for the password!</p>'; } ?> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?register=true" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form> </div> <div id="tab3_login" class="tab_content_login" style="display:none;"> <h3>Lose something?</h3> <p>Enter your username or email to reset your password.</p> <form method="post" action="<?php echo site_url('wp-login.php?action=lostpassword', 'login_post') ?>" class="wp-user-form"> <div class="username"> <label for="user_login" class="hide"><?php _e('Username or Email'); ?>: </label> <input type="text" name="user_login" value="" size="20" id="user_login" tabindex="1001" /> </div> <div class="login_fields"> <?php do_action('login_form', 'resetpass'); ?> <input type="submit" name="user-submit" value="<?php _e('Reset my password'); ?>" class="user-submit" tabindex="1002" /> <?php $reset = $_GET['reset']; if($reset == true) { echo '<p>A message will be sent to your email address.</p>'; } ?> <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>?reset=true" /> <input type="hidden" name="user-cookie" value="1" /> </div> </form> </div> </div>  <?php } else { // is logged in ?>  <div class="sidebox"> <h3>Welcome, <?php echo $user_identity; ?></h3> <div class="usericon"> <?php global $userdata; get_currentuserinfo(); echo get_avatar($userdata->ID, 60); ?>  </div> <div class="userinfo"> <p>You&rsquo;re logged in as <strong><?php echo $user_identity; ?></strong></p> <p> Log out |  <?php if (current_user_can('manage_options')) {  echo '' . __('Admin') . ''; } else {  echo '' . __('Profile') . ''; } ?>  </p> </div> </div>  <?php } ?>  </div>

Okay, so here are the functional highlights for this hefty chunk of code:

  • Everything is wrapped with <div id="login-register-password"></div>
  • If the user is not logged in, the three forms are included in the markup
  • If the user is logged in, a simple welcome message is displayed
  • Success messages are displayed after both password recovery and registration
  • Each form submission sets a generic user-cookie
  • After login or registration, the script redirects the user to the same page
  • Only one form is shown at a time; JavaScript is used to show and hide forms

So if you just throw this thing into your sidebar.php file, you’ll see the login form and three links: login, register, and recover-password. The other two forms are included in the markup, but are hidden with CSS (display:none;). As-is, the three links won’t do anything because they require JavaScript to work. Once we sprinkle in some jQuery, the links will toggle the three different forms.

Implement and Demo

First let’s walk through using this code in your theme, and then we’ll check out a demo.

  1. Place the custom login code in your sidebar.php file, or some other location
  2. Grab the jQuery code (no-conflict mode) and include it in your footer.php file
  3. Include the CSS code in your theme’s style.css file, or wherever your styles are located

..and done. Once these three items are in place, upload everything to your server and check it out. Here is a demo showing this code used on a custom page within the loop.

Note that we have registration disabled here at DigWP.com, so the forms won’t be of much use other than to show how the tabs work and how the forms are styled. Here are some screenshots showing the “success” messages, and also the “logged-in” display.

[ Screenshot: Registration Success Message ]
Message displayed on successful registration

[ Screenshot: Password-Recovery Success Message ]
Message displayed on successful password-recovery

[ Screenshot: Logged-In View ]
Content displayed when user is logged into the site

Here again is the demo and here are the three resource files:

Customizing things

One of the main reasons why we like working with actual code instead of widgets or plugins is the ability to easily customize anything we want exactly how we want it. With this implementation, there are basically three things to customize: the jQuery, the custom login code, and the CSS. As with any code, there are countless ways to modify appearance and functionality, so hopefully you have something specific in mind, and are familiar enough with design to jump in and customize things. If not, no worries – here are some ideas to help get you started.

Customizing the login forms

As-is, the custom login forms redirect to the current page. To get any/all of the forms to redirect to a different page, locate and edit the following line of code:

<input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />

There are three instances of this hidden input field, which WordPress uses to perform the redirect. The value returns the current URL, so that’s what needs changed for each form to redirect elsewhere.

Another useful modification involves customizing what the logged-in users see. Showing the gravatar and username is kind of neat, but there are tons of cool tricks to help ensure smooth user experience.

Customizing the jQuery

The jQuery used to show/hide the different login panels is actually pretty basic and is only used for toggling display states. I suppose there is a way to customize this, but it already handles additional menu items, so maybe you want to change the class names or optimize the script or something.

I do love to look at a nice slice of well-formatted jQuery, however, so I’ll further indulge myself by including the entire code snippet right here:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $(".tab_content_login").hide(); $("ul.tabs_login li:first").addClass("active_login").show(); $(".tab_content_login:first").show(); $("ul.tabs_login li").click(function() { $("ul.tabs_login li").removeClass("active_login"); $(this).addClass("active_login"); $(".tab_content_login").hide(); var activeTab = $(this).find("a").attr("href"); if ($.browser.msie) {$(activeTab).show();} else {$(activeTab).show();} return false; }); }); </script>

A bit heavy-handed perhaps, but works great with no editing required ;)

Customizing the CSS

To get that fancy tabbed form menu looking all clean and rounded, much CSS is used. So instead of posting endless gobs of CSS, here is the code in plain text. As with any CSS, the best way to customize things is to open Firebug and start tweaking.

Just the links

One last trick: use this code to display links to the default WordPress login/registration page:

<ul> <?php wp_register(); ?> <li><?php wp_loginout(); ?></li> <?php wp_meta(); ?> </ul>

Nothing to golf about, but I figured it was worth mentioning.

Wrap up

Using the code and techniques in this article, you can provide your readers with a login form anywhere on your site. This makes it easy for your users and visitors to login/logout, register for your site, and recover passwords without leaving their current page. The login code works great as-is or is easily transformed into a snazzily tabbed login menu using a sprinkle of CSS and a dash of jQuery.

Finally, one of the great things about WordPress is that there is always more than one way to set things up. So if you see a way to improve the code, please share with the community by leaving a comment. Thank you!

Possibly Related Posts

Make it easy for your wp users to login, register and/or grab their lost password. Put the login, register, recover password code anywhere in your theme.

Posted via email from rightinfo's posterous

No comments: