Creating custom user registration form in Drupal 7

Creating custom user registration form sometimes can be a little tricky, but lets try to create a custom registration form in Drupal 7 with just a few steps.

First we add a few lines of codes to template.php as follows

function themename_theme($existing, $type, $theme, $path){
  $hooks['user_register_form']=array(
    'render element'=>'form',
    'template' =>'templates/user-register',
  );
return $hooks;
}

function themename_preprocess_user_register(&$variables) {
  $variables['form'] = drupal_build_form('user_register_form', user_register_form(array()));
}

Then creating the user-register.tpl.php and place it inside templates folder in your theme, write the following in your user-register.tpl.php:

<?php 
  print render($form['form_id']);
  print render($form['form_build_id']);
  print render($form['account']['name']);
  print render($form['account']['mail']);
  print render($form['field_yourcustomfield']); // Your Drupal 7 profile field if any.
  print drupal_render($form['actions']); 
?>

Clear your Drupal cache and visit your user registration form to see the changes.


Author: danpros

A self-proclaimed geek, business owner, and part-time blogger.

Comments on “Creating custom user registration form in Drupal 7”