Making a Fancy WordPress Register Form from Scratch

In this tutorial, I will guide you through the process of making a beautiful “Register” form, using Fancybox, jQuery, and, of course, WordPress. As you’ll find, the process is really quite simple.

Step 1. The Markup

First, let’s place our button at the top of the page, replacing the default description in the theme.
  1. <div id="registration"><a class="show register-button" href="#register-form">Register</a></div>  
Notice that in the register button, the href (#register-form) is the same ID as the form below. We’re also using the class “.show” to call FancyBox with jQuery.
We need our base; let’s create our markup. Open header.php, and place this following snippet anywhere you’d like.
  1. <div style="display:none"> <!-- Registration -->  
  2.         <div id="register-form">  
  3.         <div class="title">  
  4.             <h1>Register your Account</h1>  
  5.             <span>Sign Up with us and Enjoy!</span>  
  6.         </div>  
  7.             <form action="" method="post">  
  8.             <input type="text" name="" value="Username" id="" class="input"/>  
  9.             <input type="text" name="" value="E-Mail" id="" class="input" />  
  10.                 <input type="submit" value="Register" id="register" />  
  11.             <hr />  
  12.             <p class="statement">A password will be e-mailed to you.</p>  
  13.             </form>  
  14.         </div>  
  15. </div><!-- /Registration -->  
Note that I’m using display:none to hide the form initially.

Step 2. CSS

The CSS is rather simple; I’m merely styling a quick form design in PhotoShop.
The form, minus the styling, looks like so: (note that I’ve removed the display:none in the markup to check my styles)
Let’s next begin styling our box.
  1. div#register-form {  
  2.     width400px;  
  3.     overflowhidden;  
  4.     height230px;  
  5.     positionrelative;  
  6.     background#f9f9f9 url(images/secure.png) no-repeat 260px 40px;  
  7.     font-familyHelvetica Neue, HelveticaArial !important;  
  8. }  
Continuing on, I’ll now style the text inputs, adding some fanciness.
  1. div#register-form input[type="text"] {  
  2.     displayblock;  
  3.     border1px solid #ccc;  
  4.     margin5px 20px;  
  5.     padding9px 4px;  
  6.     -moz-border-radius: 4px;  
  7.     -webkit-border-radius:4px;  
  8.     width200px;  
  9.     font-familyHelvetica Neue, HelveticaArial !important;  
  10. }  
  11. div#register-form input[type="text"]:hover {  
  12.     border-color#b1b1b1;  
  13. }  
  14. div#register-form input[type="text"]:focus {  
  15.     -moz-box-shadow: 0 0 3px #ccc;  
  16.     -webkit-box-shadow: 0 0 3px #ccc;  
  17. }  
Now, I’ll style the button, adding a hover state, and replacing the default button with an image.
  1. div#register-form input[type="submit"]#register {  
  2.     backgroundurl(images/register.jpg) no-repeat;  
  3.     border: 0;  
  4.     clearboth;  
  5.     cursorpointer;  
  6.     height31px;  
  7.     overflowhidden;  
  8.     positionrelative;  
  9.     left:295px;  
  10.     text-indent: -9999px;  
  11.     top:42px;  
  12.     width:92px;  
  13. }  
  14. div#register-form input[type="submit"]#register:hover {  
  15.     background-position: 0 -32px;  
  16. }  
Finally, we add some general styling.
  1. div#register-form span {  
  2.     displayblock;  
  3.     margin-bottom22px;  
  4. }  
  5. div#register-form div.title {margin-left:15px}  
  6. div#register-form div.title h1,  
  7. div#register-form div.title span {text-shadow:1px 1px 0px #fff}  
  8. div#register-form div.title h1 {  
  9.     margin:7px 0;  
  10. }  
  11. p.statement {  
  12.     position:absolute;  
  13.     bottombottom:-2px;  
  14.     left:10px;  
  15.     font-size:.9em;  
  16.     color:#6d6d6d;  
  17.     text-shadow:1px 1px 0px #fff;  
  18. }  
Voila! we have our form. Now, let’s move forward with the jQuery functionality.

Step 3. jQuery

First, we need to include jQuery within WordPress. To achieve this, we need to place the following chunk of code before the <head> tag within the header.php file. Remember, as WordPress itself utilizes jQuery, we don’t want to potentially load it twice!
  1. <?php wp_enqueue_script("jquery"); ?>  
  2. <?php wp_head(); ?>  
Download Fancybox and place it in your WordPress folder. To organize things a bit more, I’ve created an “Includes” folder.
Next, open your footer.php file, and place the following before the end of the </body> tag
  1. <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/includes/fancybox/jquery.fancybox-1.3.1.css" media="screen" />  
  2.     <!-- Javascript -->  
  3. <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/includes/fancybox/jquery.mousewheel-3.0.2.pack.js"></script>  
  4. <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/includes/fancybox/jquery.fancybox-1.3.1.pack.js"></script>  
And now, let’s call the fancybox method; paste this after the code above and before the closing body tag.
  1. jQuery(document).ready(function() {  
  2.     jQuery(".show").fancybox({  
  3.         'titleShow'     : 'false',  
  4.         'transitionIn'      : 'fade',  
  5.         'transitionOut'     : 'fade'  
  6.     });  
  7. });  
We’re done! Our form has been created; we lastly just need to pass the necessary WordPress information to make it function properly.

Step 4. WordPress

There is nothing fancy here; we only require two WordPress snippets, hidden within the wp-login.php file.
The first snippet:
  1. <?php echo site_url('wp-login.php?action=register''login_post') ?>  
And:
  1. <?php do_action('register_form'); ?>  
The final code should look like so:
  1. <div style="display:none"> <!-- Registration -->  
  2.         <div id="register-form">  
  3.         <div class="title">  
  4.             <h1>Register your Account</h1>  
  5.             <span>Sign Up with us and Enjoy!</span>  
  6.         </div>  
  7.             <form action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">  
  8.             <input type="text" name="user_login" value="Username" id="user_login" class="input" />  
  9.             <input type="text" name="user_email" value="E-Mail" id="user_email" class="input"  />  
  10.                 <?php do_action('register_form'); ?>  
  11.                 <input type="submit" value="Register" id="register" />  
  12.             <hr />  
  13.             <p class="statement">A password will be e-mailed to you.</p>  
  14.             </form>  
  15.         </div>  
  16. </div><!-- /Registration -->  
Please note that it’s really important, and necessary, to have user_login as a name and as an ID in your text input; the same is true for the email input. Otherwise, it won’t work.
And with that, we’re done!

Conclusion

With a touch of code, and some tweaks, we’ve managed to build a great looking “Register Form” for our users. What do you think?

No comments:

Post a Comment