Last updated May 7, 2011. Created on June 24, 2007.
Edited by Heihachi88, fmitchell, lisarex, jhodgdon. Log in to edit this page.

description

These snippets allow you to override the default login layout using a custom user_login.tpl.php and control each element of the login form.

If you want to customise the full page layout, click through to the Customising the login, registration and request password full page layout handbook page.

Step 1 of 2

In a text editor like notepad.exe, create a file called template.php using the the following snippet. If you already have a template.php file, simply add it to your existing one (remembering to omit the opening and closing PHP tags).

<?php
 
/**
   * This snippet catches the default login form and looks for your
   * custom user_login.tpl.php file in the theme folder
   */
 
function phptemplate_user_login($form) {
 
$form['pass']['#description'] = 'password';
 
$form['name']['#description'] = 'username';
 
$form['name']['#size'] = 10; // change the size of the username form box
 
$form['pass']['#size'] = 10; // change the size of the password form box
 
$form['submit']['#value'] = 'Log in'; // change the login button text.

 

return _phptemplate_callback('user_login', array('form' => $form));
}
function 
_phptemplate_variables($hook, $vars) {
  global
$user;
  switch(
$hook) {
          case
'page' :
       
// SUPER IMPORTANT: These rules prevent you from crashing your server :-)
       
if (arg(0) != "user" && $user->uid == "0") {
           
$vars['user_login_form'] = user_login($msg = '');
        }
        break;
  }
  return
$vars;
}
?>

For reference, here's the array of variables available to the login form for username and password.

            [#type] => password
            [#title] => Password
            [#description] =>
            [#required] => 1
            [#attributes] => Array
                (
                    [tabindex] => 2
                )

            [#tree] =>
            [#parents] => Array
                (
                    [0] => pass
                )

            [#weight] => 0.001
            [#input] => 1
            [#size] => 30
            [#name] => edit[pass]
            [#id] => edit-pass
            [#value] =>

Step 2 of 2

  1. In a text editor paste the following snippet into your user_login.tpl.php file
  2. Edit the layout and classes to suit
  3. Upload your edited user_login.tpl.php to your active theme folder

For use with Drupal 4.7.x

<?php 
 
// If the user is already logged in, go to the user page instead
 
global $user;
  if (
$user->uid) {
   
drupal_goto('user/'. $user->uid);
  }
?>

<div id="user_login">
<?php print form_render($form['name']);// this outputs the password form field ?>
<?php print form_render($form['pass']); // this outputs the password form field ?>
<?php print form_render($form); // this renders the form ?>
<p><a class="textlink" href="?q=user/password">Forgot Password?</a>
</div>

For use with Drupal 5.x

The call form_render changed to drupal_render in Drupal 5.x

<?php 
 
// If the user is already logged in, go to the user page instead
 
global $user;
  if (
$user->uid) {
   
drupal_goto('user/'. $user->uid);
  }
?>

<div id="user_login">
<?php print drupal_render($form['name']);// this outputs the username form field ?>
<?php print drupal_render($form['pass']); // this outputs the password form field ?>
<?php print drupal_render($form); // this renders the form ?>
<p><a class="textlink" href="?q=user/password">Forgot Password?</a>
</div>

Style sheet reference

For controlling how your login form looks using your style sheet, this is what the rendered login form HTML and class names are by default:

<div class="form-item">
<label for="edit-name">Username: <span class="form-required" title="This field is required.">*</span></label>
<input type="text" maxlength="60" name="name" id="edit-name"  size="30" value="" tabindex="1" class="form-text required" />
<div class="description">enter your username</div>
</div>
<div class="form-item">
<label for="edit-pass">Password: <span class="form-required" title="This field is required.">*</span></label>
<input type="password" name="pass" id="edit-pass"  size="40"  tabindex="2" class="form-text required" />
<div class="description">enter your password</div>
</div>
<input type="hidden" name="form_id" id="edit-user-login" value="user_login"  />
<input type="submit" name="op" id="edit-submit" value="Log in"  tabindex="3" class="form-submit" />
<p><a class="textlink" href="?q=user/password">Forgotten your Password?</a>
</div>

notes

  • If you have an even more advanced snippet that has been tested, please add a child page to this handbook page
  • This snippet was tested with Drupal 5.x (June 24th 2007) by Dublin Drupaller (Note: If you have the locale.module enabled you may need to refresh your search_index by editing any text string related to the user page before your changes take effect, such as 'password'. )
  • If you want to create a custom login bar for the header of your site, click through to this book page titled HOWTO: Create a custom user login bar.
  • Recommended reading for more information and an explanation of how the snippets work: Dominating the User Login Form at Nick Lewis' blog

Drupal 6

Here is a links for how to do this in Drupal 6:

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

yokosatan’s picture

I'm new drupal developer, so I don't understand how to do and this link
http://www.webcoder.kz/temizatsiya-drupalloginblock
helps to know more what is it but it not work. I think I do something wrong. I will try other way to do log in block.
Other tutorial are good but I not understand or I should to do some basic first.