Community Documentation

Customize The Default User Registration Form

Last updated September 9, 2012. Created by carwin on September 9, 2012.
Log in to edit this page.

Firstly, in your theme's root directory add the following to template.php. If the file does not exist, create it.

<?php
function YOURTHEME_theme(&$existing, $type, $theme, $path){
 
$hooks = array();
  
// Make user-register.tpl.php available
 
$hooks['user_register_form'] = array (
    
'render element' => 'form',
    
'path' => drupal_get_path('theme','YOURTHEME'),
    
'template' => 'user-register',
    
'preprocess functions' => array('YOURTHEME_preprocess_user_register_form'),
  );
  return
$hooks;
}

function
YOURTHEME_preprocess_user_register_form(&$vars) {
 
$args = func_get_args();
 
array_shift($args);
 
$form_state['build_info']['args'] = $args;
 
$vars['form'] = drupal_build_form('user_register_form', $form_state['build_info']['args']);
}
?>

Account fields

Account fields are rendered following the pattern of the two required fields, 'name' and 'mail':

<?php
print render($form['account']['name']);
print
render($form['account']['mail']);
?>

Profile2 Support

To load Profile2 fields into the user registration form manually, use this pattern:

<?php
print render($form['profile_theprofile_name']['field_thefield_name']);
?>

Also, I make use of field group module (here) with Profile2 and noticed that I didn't need to include anything about the field group a Profile2 field is in to render that field.

CAPTCHA

If you use the CAPTCHA module and need to build your own user-register.tpl.php you'll need this to render it:

<?php
 
print render($form['captcha']);
?>

This took me longer than it should of to figure it out.

Submission

Lastly, if you want your form to actually be usable, the following snippet is required to build the form action stuff:

<?php
 
print drupal_render($form['actions']);
  print
drupal_render_children($form);
?>

This page is a reference page spawned from this support thread in the Theme Development forum.

Comments

Note: Regarding profile2 form

Note: Regarding profile2 form elements and in order for them to be available, you have to tick the checkbox Show during user account registration. under your profile type. Otherwise the $form['profile_name'] won't be available.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Designers/themers, Site builders
Level
Intermediate
Keywords
User Register

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here