Skip to main content

Theming the User Registration Form in Drupal 6

Step One: Tell Drupal What Template to Use

There are 2 steps you need to take to implement a better looking user registration page within your Drupal site - create a template file, used to display the contents of the form, and make some small changes to template.php within your theme file to tell Drupal which template to use. We will look changes to template.php first, you will be modifying a function called 'themename_theme' (substitute the name of your theme for 'themename' in that statement). If this function is not already there, create it using the example below. Here, we are passing on the name of the template file for the user registration form, and calling it 'user-register'. You can feel free to use any name you wish for the template file.

<?php
function themename_theme($existing, $type, $theme, $path) { return array( ... // tell Drupal what template to use for the user register form 'user_register' => array( 'arguments' => array('form' => NULL), 'template' => 'user-register', // this is the name of the template ), ... );
}
?>

After that is done, reset the registry by clearing cached data at the bottom of the Administer > Performance page.

Step Two: Build Your Template

The next step is you will want to create the template for the user registration form itself. In your theme directory, create a filed called user-register.tpl.php. Notice how the name exactly corresponds to the name we used in the code for template.php. If your form is not displaying properly, there is a good chance it is because there is a mispelling in one of these places. The following example shows you how to create some basic template code that will display the username and password fields, along with the submit button. Within user-register.tpl.php, paste the following code:

<?php print drupal_render($form['account']['name']); // prints the username field
?>
<?php print drupal_render($form['account']['pass']); // print the password field
?>
<?php print drupal_render($form['submit']); // print the submit button print '<div style="display:none">'. drupal_render($form).'</div>';// form id and extra fields if any
?>

At this point, Drupal should know to use your template for generating the user registration page. Season to taste with some CSS and you are on your way to a great looking registration page!

As your site grows more complex, you may need to add additional fields to the form. The drupal_render function tells Drupal to 'build' any field that has been defined for this form. If you ever need to get a list of all the form fields that are part of the user registration form, put the following code anywhere within your template to generate a list of fields that are 'defined' for it.

<?php print '<pre>'; print var_export($form); print '</pre>';
?>