How to: Add fields to the Joomla registration form

5 02 2010

For the most part of this week, I was working on the i-Sana website.  If you’re interested, it’s a Dutch website that offers a free digital magazine (or e-magazine) on Health.  Anyway, this was an important project for me and  I wanted to do everything just right.  So, when the bosses requested that we collected the ZIP codes of users, I tried to find a solution.

Since we’re going to be using AcyMailing to send our newsletters, we wanted to use the “core” Joomla registration form, which meant that we had to add a zip code field to it.  We pulled it off, of course.  I’m not being snobbish, but you know by now that I only discuss “problems” when I managed to solve them, right?

Anyway, below you can find the solution to add custom fields to the registration form yourself.

The steps

1. Edit your Joomla database: The first step is to edit your Joomla database; because you’ll need to store the input for your new field somewhere.  So,  open your  database using phpmyadmin (or another solution; I prefer phpmyadmin myself as it’s a tool that many ISP).  Then, make a back-up of the table (something)_users

Add a field to the table (something)_users.  The field has to be a text field.  Choose a field name that’s related to the extra field you want to create in your form; as you’ll be using this name in the next steps. 

2. Editing the registration form: To add the new field to the registration form, you’ll need to edit the following file:

/components/com_user/views/register/tmpl/default.php

In the part of the code where the form is defined, add the following code:  
<tr>

<td height="40">

<label id="zip" for="postcode">

<?php echo JText::_( ‘postcode’);?>:

</label>

</td>

<td>

<input class="inputbox required" type="text" id="postcode" name="postcode" size="40">

</td>

In this example, a new text field is added for “Postcode”.  You’ll want to changet the references to post code to the field name you used in the previous step.

3. Changing the registration logic: To ensure that the input is written to the Joomla database, you need to edit the following file:

libraries/joomla/database/table/user.php

Under “class JTabeUser extends Jtable”  you’ll see the declaration of variables.  Add the following code:

"var $postcode = null;"

SUPER IMPORTANT WARNING:  the code above must be typed EXACTLY as shown.  I accidentaly forgot the space after the =, and I broke the ENTIRE registration process; wondering where I made a mistake. 

Of course, you’ll want to change “postcode” to the value you gave your field in step one.

4. Editing the user profile in the back-end: The extra data are now being stored in the database, but you’ll also want them to show up in the Joomla back-end.  To achieve this, edit the following file:  

/administrator/components/com_users/views/user/tmpl/form.php

Under <form action="index.php" method="post" name="adminForm" autocomplete="off">, add the following code :

<tr> <td class="key">

<label for="postcode"> <?php echo JText::_( ‘Postcode’); ?></label>

</td>

<td> <input class="textbox" type="text" name="postcode" id="postcode" size="40" value="<?php echo $this->user->get(‘postcode’);?>"/>

</td>

</tr>

As usual, replace ‘postcode’ with your own variable!

5.  Editing the user profile in the front-end:  (added: 17/02/2010)  If you want to see the extra data in the user profiles in the front end of the site, you’ll need to make changes to the following file:

/components/com_user/views/user/tmpl/form.php 

Under <table cellpadding="5" cellspacing="0" border="0" width="100%">; you’ll see that the form is being defined again.  Add the following code: 

<tr>
   <td>
    <label for="postcode">
    <?php echo JText::_(‘Postcode’);?></label>
    </td>
   <td> <input class="inputbox required" type="text" id="postcode" name="postcode" size="40" value="<?php echo $this->escape($this->user->get(‘postcode’));?>" >
</td>
</tr>

Once again, you’ll want to change “postcode”.

And bam!  You’ve added your field to the registration procedure of Joomla.  

(last edited on 17/02/2010.  Added “Editiing the user profile in the front-end after comment on the Joomla Forum)


Actions

Information

3 responses

5 02 2010
Toretto's corner » How to: Add fields to the Joomla registration form

[...] [This post was originally posted on joomla-and-more, a site of the Toralko group.] [...]

20 02 2010
Update on “How to Add fields to the Joomla registration form « Joomla and more

[...] You can read the updated post here [...]

21 02 2010
Superman

Very helpful guide. Thanks a lot!

Leave a comment