How to Create a WordPress Login Form Overlay

login-overlay1

WordPress is more than just a blogging tool. It can be used as a Content Management System (CMS) for many different purposes. It has a built in system for most things you would have to code yourself, or pay for an expensive custom application. With a little creativity, you can use basic features to power almost anything you want.

One of the most important things to a website with any hopes of establishing a user connection is having user accounts.

Now, blogs get by here by having comments tied to emails, which is similar in a way. But you’re most likely looking to have unique accounts for your site. This was every user can login, logout, edit their profile and have special privileges. WordPress also allows you (as an admin) to control the level and permissions of all other users.

Login Form Overlay

So what exactly is a “login form overlay”? Basically it’s a simple and quick way for users to login from the page that they are on at any given moment, and come back to that page without losing a second. Just open the form, login, and you’re back!

For more experienced users, the question arises: Does this use AJAX? No it doesn’t. We’ll be using a custom form, but putting it through WordPress’ own file, and then redirecting back to where the user was. Here’s the things we’ll be using:

  • Lightbox Gone Wild, a modification to LightboxJS done by ParticleTree. This is going to create the overlay where our form will go. It actually opens a new file above the current page.
  • Your file editor of choice.

Other than that we’ll be writing our own code, or using code we already have from WordPress.

Example

I have this live in action on my own site. Click here to see the comment form. You don’t have to sign up to use it, but it will show you the overlay.

Setting up Your Theme

We’re going to be editing the actual WordPress theme here. Depending on where you want to put the overlay, that will determine which file you edit. On my site I have it so if a user isn’t logged in, they can bring up the form right above the comment form.

Go to the Lightbox Gone Wild site and download the files. Now upload the “lightbox” folder to your theme folder, and in your header.php file, add the following code before </head>.

1
2
3
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ?>/lightbox/css/lightbox.css" type="text/css" />
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/lightbox/scripts/prototype.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/lightbox/scripts/lightbox.js"></script>

That’s just going to call the files we need to use the overlay. You can see that it uses Prototype, a Javascript framework you might already have. Check your header.php file before and remove the second line from above if you do have it. If you aren’t sure, don’t worry about it and leave everything the way it is!

The Login File

This is where we’re going to create the form we need to login. We’ll create just a basic HTML form with some PHP, mostly by copying code straight from the source of the regular WordPress login page.

Copy and paste the code below into a new file, name it login.php and upload it to your theme folder. I’ll talk about the additions after the block.

Make sure you change http://www.yourblog.com in the second line to your blog’s address (Including the http:// and with no trailing slash!).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<? 
$blogurl = "http://www.yourblog.com";
$postlink = $_GET['redirect']; 
?>
 
<link rel="stylesheet" href="<?=$blogurl ?>/wp-admin/css/login.css" type="text/css" />
 
<div id="login">
 
<form name="loginform" id="loginform" action="<?=$blogurl ?>/wp-login.php" method="post">
	<p>
		<label>Username<br />
		<input type="text" name="log" id="user_login" class="input" value="" size="20" tabindex="10" /></label>
	</p>
	<p>
		<label>Password<br />
		<input type="password" name="pwd" id="user_pass" class="input" value="" size="20" tabindex="20" /></label>
 
	</p>
	<p class="forgetmenot"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever" tabindex="90" /> Remember Me</label></p>
	<p class="submit">
		<input type="submit" name="wp-submit" id="wp-submit" value="Log In" tabindex="100" />
		<input type="hidden" name="redirect_to" value="<?=$postlink ?>#respond" />
		<input type="hidden" name="testcookie" value="1" />
	</p>
</form>
 
<p id="nav">
<a href="<?=$blogurl ?>/wp-login.php?action=lostpassword" title="Password Lost and Found">Lost your password?</a>
</p>
 
</div>
 
<p id="backtoblog"><a href="#" class="lbAction" rel="deactivate">Close</a></p>
 
<script type="text/javascript">
try{document.getElementById('user_login').focus();}catch(e){}
</script>

First, we have to note that we can’t access WordPress’ functions in this file. We’re loading it on its own on top of another file. This is why you have to set the URL at the top to your blog’s own. We can’t use <? bloginfo(‘url’); ?> or any other built in functionality in this file. You could try to include the necessary files to be able to do this, but we really don’t need to for this.

I’m also including the login form’s stylesheet for extra style. If you want, you can include your normal style sheet and style it your own way. I found the CSS in that file already worked with my design, so I left it.

Linking to The Form

The end is near! Now we just need to link to our form using a special syntax and it’s done. Put this where you want to have the link. Personally, I have mine in comments.php, but you can put it almost anywhere. To activate the overlay, use this link:

1
2
3
<?php if ( $user_ID ) {} else { ?>
<p><a href="<?php echo get_option('siteurl'); ?>/wp-content/themes/<?php echo get_option('template'); ?>/login.php?redirect=<?php the_permalink(); ?>" class="lbOn" title="Log in">Login &raquo;</a></p>
<?php } ?>

Notice how I’m sending a $_GET variable through to the file? It does work a little like AJAX in that way, but in the end we reload the page so all the changes for logged in users can take effect.

Help! It doesn’t work!

Well, it’s expected that you might not be able to get this running smoothly the first time if you’re not comfortable with editing your theme and creating new files within it. Here are some common errors you should double check before asking a question. I’ll answer questions in the comments, but I’d like to minimize the trouble you have to go through.

  • You didn’t properly include the files in header.php. You have to make sure these files are working with your site. Double check you uploaded them into the same place you are linking.
  • You didn’t add your URL to the login.php code. Make sure you include the http:// and have no / at the end.
  • If you changed any of the code yourself, check your edits. Also make sure you’re linking to the same place it’s uploaded. It’s in your theme’s folder, not your main folder!

What Else Can You Do?

Once you have users logging in and out, you can use WordPress and its core features to give them special content, view certain pages. One thing I made is a page where a logged in user can go to see all their comments they’ve made. It’s just a simple page template with some custom code, but that’s a story for another day.

Share

  1. Couldn’t see the overlay from your link (I’m using FF2.0.0.16 on Windows XP).

  2. Michael Martin (1319 comments)26 August 08

    Andy,
    Thanks for the warning. Fixed the link now! :)

  3. Christer (2 comments)26 August 08

    Hi,
    I Tried this on my Wp MU installment. All work’s fine except that the login form is
    not showing up in a lightbox style, more like a regular page.
    Check my URL, i’ve put the login at the top

    It seems that the Lightbox code doesn’t work correctly.
    I checked and double checked, the Lightbox folder is untouched in the themes directory

    Any ideas?

    -Christer

  4. Michael Martin (1319 comments)26 August 08

    Christer,
    The problem is in the first bit of code you had to paste in. Did you copy it over exactly as it is above?

    Instead of the proper path to the files, the code in your header looks like this:


    The href=”" part is pointing to the wrong place.

  5. Christer (2 comments)26 August 08

    Hi again,
    yeah, i figured it was there, so i made a ‘../’ to point it up one level…now i put it back and i see it looks for the lightbox folder inside my contempt template folder. I’ll just move a copy of it there.

    Thanks alot!

  6. Michael Martin (1319 comments)26 August 08

    Christer,
    No problem. Hope it works for you now. :)

  7. milo (72 comments)30 August 08

    Wrapping the /wp-login.php url in an I-Box works too,
    then the login site is loaded in a div overlay.

  8. Thanks for sharing this tutorial. I will be saving it so that I can make use of it with client websites myself.

  9. micah (3 comments)2 September 08

    Nice! I will definitely use this. Thanks!

  10. micah (3 comments)2 September 08

    Nice! I will definitely use this. Thanks!

    ——————————————————
    http://www.submitmydesign.com

  11. Ryan (8 comments)23 September 08

    Thanks for the tut – I got it to work – kinda.

    A couple of things

    1. I want to use this on the home page for returning members
    IF they are not a member how do I incorporate the registration functionality.

    2. The Log In button is there but once logged in it does not display the user id
    Once logged in the Log In link disappears but it does not give a def indication that one is logged in – Is this line of code “( !$user_ID )” supposed to display the user name or did/am i missing a step.

    3. the “remember me” function – is it supposed to remember me so that I don’t have to enter my credentials each time I visit the site or does it play a diff role? Because it is not remembering me (yes cookies are active)

    4. When I enter the wrong login info it goes to the reg wp-login page.
    Is it possible to display that “wrong log in” on the overlay.

    Overall goal here is to eliminate the WordPressyness if that makes sense

  12. Sumesh (39 comments)5 October 08

    This looks beautiful – lightbox has been my favorite – amazing without being too flashy.

    I remember WPDesigner having a tutorial on placing a login form in sidebar or elsewhere so you can log in from any where – I’d reckon that method is lighter but not as beautiful.

    Maybe you should add the login to PBD – so that we can track our comments and stuff, you know…

  13. Brian (3 comments)14 October 08

    I’ve been using lightbox 2 for my images on my blog for some time. I like this implementation of it, and i’m able to use it for a login box on my dev site. I haven’t tried yet (for fear of causing a problem), but can I use the wordpress Lighbox plugin with this technique concurrently? Is there a way for me to just use this one instead and combine the functionality?

  14. lauren (1 comments)14 October 08

    SWEET!!! got it working. just a note for other who might have the same trouble – /lightbox/ goes in “/themes/mytheme/” not “/themes/”. thanks muchly.

  15. serters (1 comments)16 October 08

    I’ve been using lightbox 2 for my images on my blog for some time. I like this implementation of it, and i’m able to use it for a login box on my dev site. I haven’t tried yet (for fear of causing a problem), but can I use the wordpress Lighbox plugin with this technique concurrently? Is there a way for me to just use this one instead and combine the functionality?

  16. Juan Hernandez (1 comments)5 November 08

    This is just a test

  17. Mauricio (2 comments)5 November 08

    Hi,

    Currently on my blog when someone wants to leave a comment they have to login first. I would like to make this process less painful using this format of your. I mean, only writting Name, Email and comments.

    Could you please let me know how to achieve this or where to find information about it?

    Thanks a lot.

    Cheers!

  18. Mauricio (2 comments)5 November 08

    Hi again, I just figured it out to do this. Anyway thank you for the post it is very useful.

  19. Sox (1 comments)14 November 08

    I followed the instructions but it doesn’t work:

    http://testsite.noquarterusa.net/

  20. Milan (1 comments)24 November 08

    Helpful post. Thank you.

  21. mikel (1 comments)29 December 08

    thank you for this tutorial

  22. If I use Lightbox 2.0 wordpress plugin which already links to lighbox.js and prototype.js on my Money Makers Portal blog how can I overlay link using it?

  23. Robin (2 comments)12 January 09

    I’m searching wordpress login form plugin for my new wordpress site, but your way can get a nice login form. It’s a easy way to add it, Thanks!

  24. tina (3 comments)16 March 09

    thank for your helpfulness
    :)

  25. LANeros (1 comments)19 March 09

    Very nice! I’m going to use this on my next blog.

  26. mssmotorrd (1 comments)3 May 09

    It’s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
    p.s. You have a very good template for your blog. Where did you find it?

    • Michael Martin (1319 comments)5 May 09

      Thanks for the complement, but sorry, this is a custom design. Can’t find it anywhere else :( I’ll probably release it eventually but not until I’ve stopped using it here! :D

  27. Auliyyaa (1 comments)10 May 09

    ok, this is great and very helpful…though actually i would like to put it at the side bar. it works! but, my photo gallery -which used lightbox type too- doesn’t work anymore… any solution??

  28. lol
    very good idea
    very good code :)

  29. John (6 comments)14 August 09

    This is great – works like a champ.

    Now, to take this one step further – if I wanted to present a signup form in a lightbox popup, do you have a sample of which code to take from wp-signup.php?

    Thanks much, Michael – nice job!

  30. Rick (2 comments)17 August 09

    I see a lot of post saying that this works but only sort of, I believe that most people are running into a conflict between JQuery and Prototype. They both use the $ selector.
    One possible solution is loading the prototype library correcty using the wp_enqueue() functon. You can find more info at the codex http://codex.wordpress.org/Fun.....eue_script

  31. login page will be HTML:

    Username:

    Password:

    (JS)javascript file code:

  32. Nice tutorial. I was looking for this.

  33. Ivan (2 comments)8 December 09

    Nice tutorial. It is working on my website. Thanks.

  34. Keller Hawthorne (1 comments)18 December 09

    Michael – this tut is fantastic and exactly what I was looking for. My only issue is that the overlay displays a blank box – none of the code from the login.php file appears. Any reason for this that you know of?

  35. Pete (2 comments)30 December 09

    Thanks for the great tutorial! Everything works great except for the redirect. After the user logs in from the Lightbox login, it redirects them to the standard login page for the blog. Is it possible to redirect to the page that the user clicked the login link from?

  36. Hydraulic (1 comments)8 January 10

    Wow, It just works fine!!!

  37. Steve (12 comments)19 January 10

    Great tutorials, thank you very much for sharing!

  38. Thanks for the code! I have been looking for these to be able to allow people to register on my blog. Good post!

  39. bujaman (1 comments)20 January 10

    Thanks for the awesome plungin, it works great! I had one questions, how I can expand this to include a registration option that opens in the lightbox? And when someone logs in, the redirect sends them to the wp-login page, not to the main site page, how can I fix that? Thanks again.

  40. sandeep (4 comments)11 February 10

    Great Tutorial ! I’m just wondering how..my name, email and url..were automatically filled..Let me know about this also.

  41. This is great, you can save time and effort every time you log in to your blog. And you can edit in just few seconds. Thanks for sharing, I will try this one too.

  42. learn seo (2 comments)3 March 10

    this is great! Thanks so much for sharing this! Now off I go to implement it. :)

  43. learn seo (2 comments)3 March 10

    Okay, so I got it to work…with exception to the lightbox feature. =( For those of you guys who got it to work, could you go to http://www.rankingchannel.com and click on the little green icon at the upper right corner of the page? I can’t seem to figure out how come it’s not opening up the nice lightbox screen.

    Any input would be EXTREMELY helpful!

Leave a Comment

Your reply will be added to the comment above (Below any other replies to this comment) -

(We DoFollow)

Not sure how to get an image with your comment?