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.
KNOWN PROBLEMS: There are two known problems when using the method below. Those problems are due to the limitations of HTML forms.
1. You can’t set a minimum required length
2. Users can enter spaces in your new field, instead of ‘real information’.
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:
In the part of the code where the form is defined, add the following code:
<label id=”zip” for=”postcode”>
<?php echo JText::_( ‘postcode’);?>:
<input class=”inputbox required” type=”text” id=”postcode” name=”postcode” size=”40″>
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:
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:
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> <input class=”textbox” type=”text” name=”postcode” id=”postcode” size=”40″ value=”<?php echo $this->user->get(‘postcode’);?>”/>
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:
Under <table cellpadding=”5″ cellspacing=”0″ border=”0″ width=”100%”>; you’ll see that the form is being defined again. Add the following code:
<?php echo JText::_(‘Postcode’);?></label>
<td> <input class=”inputbox required” type=”text” id=”postcode” name=”postcode” size=”40″ value=”<?php echo $this->escape($this->user->get(‘postcode’));?>” >
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)