Customizing and Overriding User Login page, Register, and Password Reset in Drupal 6
Customizing the user login, register, and password reset pages is fairly simple, and uses the following concepts:
- preprocessing to set variables
- registration of functions in the theme registry
- creation of one or more theme templates.
Step 1.
In the site theme directory, create or edit your template.php file.
Step 2.
Look for a function named yourtheme_theme() and either modify it to add these return values or if it doesn't exist add the following:
<?php
/**
* Registers overrides for various functions.
*
* In this case, overrides three user functions
*/
function yourtheme_theme() {
return array(
'user_login' => array(
'template' => 'user-login',
'arguments' => array('form' => NULL),
),
'user_register' => array(
'template' => 'user-register',
'arguments' => array('form' => NULL),
),
'user_pass' => array(
'template' => 'user-pass',
'arguments' => array('form' => NULL),
),
);
}
?>Notes about that code:
- Change the function name by replacing "yourtheme" with the name of your theme
- The template can be the same for all three. The example above uses a different template for each case: user-login, user-register, and user-pass
- The template names must use a dash, not an underscore
- Note: It's
user_passnotuser_password
Step 3.
Now you implement three preprocess functions. There may be more concise ways to code this, but this works very well and is easy to read, so here we go!
<?php
function yourtheme_preprocess_user_login(&$variables) {
$variables['intro_text'] = t('This is my awesome login form');
$variables['rendered'] = drupal_render($variables['form']);
}
function yourtheme_preprocess_user_register(&$variables) {
$variables['intro_text'] = t('This is my super awesome reg form');
$variables['rendered'] = drupal_render($variables['form']);
}
function yourtheme_preprocess_user_pass(&$variables) {
$variables['intro_text'] = t('This is my super awesome insane password form');
$variables['rendered'] = drupal_render($variables['form']);
}
?>Notes about that code:
- Change the function name by replacing "yourtheme" with the name of your theme
- The line
$variables['intro_text']adds the text that follows to the$variablesarray, which gets passed to the template as$intro_text - The second line renders the form and adds that code to the
$variablesarray, which gets passed to the template as$rendered
Step 4.
Create template files to match the 'template' values defined above. In this case, we only need two: user-login.tpl.php and user-register.tpl.php (make sure to use a dash, not an underscore)
Step 5.
Paste the following into user-login.tpl.php. Modify as necessary for user-login.tpl.php and user-register.tpl.php:
<p><?php print $intro_text; ?></p>
<div class="my-form-wrapper">
<?php print $rendered; ?>
</div>Step 6.
Save this file to the theme's main directory
Step 7.
Rebuild the cache. Go to Administration > Performance and click on "Rebuild Cache" on the bottom of the page.
Now, the user login page will contain the new text from the preprocess function, and the tpl.php file(s) can be modified to suit the site's needs.

For anyone using the Zen theme...
Step 2 wouldn't work for me because my template.php already had that method declared using hooks. I added to my existing mytheme_theme method the following code:
Snippet:
$hooks['user_login'] = array('template' => 'user-login',
'arguments' => array('form' => NULL)
);
My full method ended up like so:
/**
* Implementation of HOOK_theme().
*/
function jztheme_theme(&$existing, $type, $theme, $path) {
$hooks = zen_theme($existing, $type, $theme, $path);
$hooks['user_login'] = array(
'template' => 'user-login',
'arguments' => array('form' => NULL)
);
return $hooks;
}
This worked for me, when I followed the other steps listed. I hope it helps someone else as well.
Translation of text in hooks that are overridden by a template
After a little searching and reading, I found that you can use http://drupal.org/project/potx to extract translation strings that use the t() function from themes (as well as modules).
I guess this works for template files .tpl.php, phptemplate.php and javascript files, but I haven't tried it yet.
Potx creates a pot file. A translator takes this and creates something like spanish.po, russian.po. You then place the .po files in a translations subdirectory of your theme (or module).
There is additional useful reading about multilingual javascript in 'Adding A Translated String' section of: http://www.packtpub.com/article/translations-in-drupal-6
this works for mysite.com/user, but not for mysite.com/
I am upgrading a Drupal 5.x site, and I have everything working great except for the custom login page. On the older Drupal 5.x site, I used a callback function (now depreciated in D6) so that when I visit my site at http://www.mysite.com/, the custom login page appears (it also is present if I go to http://www.mysite.com/user). But with Drupal 6, I cannot seem to get that same functionality. I must go to http://www.mysite.com/user to get the custom page. That is not acceptable; I need the login page to appear for all non-authenticated users accessing the root of the site.
What am I missing??
Now it works!
The solution is to override user_login_block ( not user_login ):
function YOUTHEME_theme(&$existing, $type, $theme, $path) {
$hooks = zen_theme($existing, $type, $theme, $path);
$hooks['user_login_block'] = array(
'template' => 'user-login',
'arguments' => array('form' => NULL)
);
// @TODO: Needs detailed comments. Patches welcome!
return $hooks;
}
function YOUTHEME_preprocess_user_login_block(&$variables) {
$variables['intro_text'] = t('This is my awesome login formX');
$variables['rendered'] = drupal_render($variables['form']);
}
Please somebody let me know if it is the right solution!
Thanks
Stefano
Soution for login forms
Look at Create a custom user login bar article.
Misleading description
I believe the description for what this code snippet does is misleading...
"Customizing the user login, register, and password reset pages is fairly simple"
This code doesn't customize the PAGE, it customizes the content area of the page, mainly the form for user-login , register, etc.
Impose a theme folder hierarchy
Once you start using template files in earnest your theme folder soon becomes unruly.
Some form of folder hierarchy soon becomes a necessity.
Drupal will search subfolders in your theme when searching for template suggestions.
When using hook_theme you can direct the template to a forms folder within your theme.
At present my theme contains the following folder hierarchy within my_theme/templates:
blocks - block.tpl.php and block overrides
boxes - box.tpl.php
comments - comment.tpl.php
forms - login, register and password form templates
nodes - node.tpl.php and custom CCK node templates
pages - page.tpl.php and custom page templates
views - views search templates, table templates and exposed form templates
The root of my_theme now need only contain: my_theme.info, style.css and template.php.
Refresh the theme registry after reorganising your templates.
<?php/**
* Registers overrides for various functions.
*
* In this case, overrides three user functions
*/
function yourtheme_theme() {
return array(
'user_login' => array(
'template' => 'templates/forms/user-login',
'arguments' => array('form' => NULL),
),
'user_register' => array(
'template' => 'templates/forms/user-register',
'arguments' => array('form' => NULL),
),
'user_pass' => array(
'template' => 'templates/forms/user-pass',
'arguments' => array('form' => NULL),
),
);
}
?>
So what would you enter in
So what would you enter in the user-login, user-register and user-pass tpl pages ?
For those that arent aware,
For those that arent aware, you can extend this lesson to create a custom page for each of the various forms.
So, user-register.tpl.php would have a corresponding page.tpl.php file of page-user-register.tpl.php. This allows you to have complete control over the user registration page/form.
is there a possibility to
is there a possibility to combine login/password/register on one page?
In response to the question
In response to the question about getting the login and registration forms on one page I did it by creating two blocks. In the login block all you have to do is insert this:
<?php print drupal_get_form('user_login')?>for the registration fom insert this into a bock:
<?php print drupal_get_form('user_register'); ?>If you wanted to set them side by side insert both blocks into the content region and theme with css - set width to something like 45% for both and float accordingly
You can see an example here