Customizing the User Login Page in Drupal 7

I was recently tasked with theming a customized user login page in Drupal 7. I could not find a whole lot of documentation for this so the first place I looked was in the core modules folder hoping to find something like user--login.tpl.php that I would be able to copy and put in my theme folder for an override. No such luck, I looked in /modules/user but nothing there I could use. Next I looked around drupal.org and found some vague references to doing this in scattered posts. This took me a several hours but finally I pieced together code from a bunch of posts and came up with a method that worked well for me.

A Custom Themed Drupal Login Page

The Code

You can add this code to your theme's template.php file where "your_themename" is the machine name of your theme. Here we tell Drupal that we want a user-login.tpl.php file in our theme folder. 

function your_themename_theme() {
  $items = array();
  // create custom user-login.tpl.php
  $items['user_login'] = array(
  'render element' => 'form',
  'path' => drupal_get_path('theme', 'your_themename') . '/templates',
  'template' => 'user-login',
  'preprocess functions' => array(
  'your_themename_preprocess_user_login'
  ),
 );
return $items;
}

You can now create a user-login.tpl.php and put it in your theme folder or the theme's template folder if there is one. Note that there are no "double dashes" in this file name as is typically traditional with Drupal 7 template file names. 

A Few Ways to Theme it.

The easy way to theme our custom user login is to simply render all the fields at once in your newly created user-login.tpl.php file with this:
<?php print drupal_render_children($form) ?>

This is probably fine in some cases but I needed to have something more granular. For this, I print out the individual variables at the top of my user-login.tpl.php file to see what's available for theming. 

<?php
  /* print the variables if needed to allow for 
  individual field theming or breaking them up. */
  print '<pre>';
  print_r($variables);
  print '</pre>';
?>

Now clear your cache and reload /user/login and the variables should print out. You will see a huge mostly undecipherable list of variables here. What we are looking for are the top level array items. These will include:

[name] => Array
[pass] => Array
[form_build_id] => Array
[form_id] => Array
[actions] => Array

Themable Output

I can covert these into themable output as such:

 <?php
 /* split the username and password from the submit button 
   so we can put in links above */

    print drupal_render($form['name']);
    print drupal_render($form['pass']);
    print drupal_render($form['form_build_id']);
    print drupal_render($form['form_id']);
    print drupal_render($form['actions']);

?>

Well, you can see where this is going, you can really theme this page however you want now that all the fields are separate. I also needed to add an image to this page so I can do that using the theme path for my image.

<div class="login-photo">
    <img src="<?php print base_path() . drupal_get_path('theme', 'your_themename') 
   . '/images/login-photo.jpg'; ?>" 
        alt="Login" title="Login" width='327' height='221' />
</div>

Again, be sure to change your_themename to the machine name of your theme. I've attached the finished theme file below so to use if for Drupal 7, you simply need to add in your template.php code above and then theme it however you want. Note, to achieve the screen capture above of my custom login page, I used a little module I created to remove the login tabs so I could have a more simple and elegant look to the page. I was now able to print those links just above the login button. 

Tags: 
  • Drupal
  • Theming
  • PHP
  • Tutorial
  • Drupal Planet

Files 

AttachmentSize
user-login.tpl.php (tar.gz file)803 bytes

Comments

Just leaving a comment to say thanks, exactly what I was looking for. I hope they make these type of changes easier within version 8, will you guys be making the switch?

Hi Simon, glad this helped. I probably won't start to develop production sites with Drupal 8 until much later next year if not early 2014...

i have a really basic question... i'm new to drupal so apologize in advance... in your code you mention modifying code to theme name so where

<div class="your-themename-user-login-form-wrapper">

would be

"<div class="zen_theme-user-login-form-wrapper">

and

<img src="<?php print base_path() . drupal_get_path('theme', 'your_themename') ?>

would be

<img src="<?php print base_path() . drupal_get_path('theme', 'zen_theme') ?>

Sort of, wherever you see "your_themename", yes that's the machine name of your theme. The machine name is based on the folder name of your theme in /sites/all/themes or /sites/yourdomain.com/themes/. So if my theme name is "My Theme", the machine name would probably be my_theme in the theme folder but it could very well be called mytheme as well.

How would I go about modifying the entire page, and not just the form? For example, on the registration page I may want a very different layout

Offhand, I am not sure, I'd have to look into to it, not sure when I will have time. Thanks.

This was awesome. Worked no problems but I want to change the register form as well. I can use the _theme function on either one or the other but not both as I don't know how to write the code.
Any help?

You would use the same exact process and create a "user-register-form.tpl.php".

Figured it out, I was calling the _theme function twice instead of combining the two $items in the same function. Now my login pages finally look remotely non-drupal! Thanks danny.

Hi Luke, great!

By the way, if you install the devel module (one that every theme developer should have anyway) then you can use the dpm() function to out put that $variables array, as it does a much more beautiful job than what you did, just saying.

Thank you so much for this article.I'd been searching for days

Thanks so much for sharing this!

Hello working for user/login page but what about user/1/edit the user profil form page

triyng something like this

<?php
function MYTHEME_theme($existing, $type, $theme, $path) {
  $items = array();
 
  $items['user_profile_form'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'MYTHEME') . '/templates',
    'template' => 'user-profile-form'
  );
  return $items;
}
?>

and create the template file user-profil-form.tpl.php Put it inside the template file

<?php
print "TEST";
print drupal_render($form);
?>

But it's not working Thank you for your help or it's impossible ?

Add new comment