Add Acceptance Terms to the Joomla! 1.5 Registration
Hannes shows how he created HP Registration
Written by Hannes Papenberg
Hello and welcome to our little workshop "How to improve your Joomla! registration". We all know that the user registration in Joomla! is not the greatest in the world, but for most people it only takes that little bit more. To show you how easy it is to extend the Joomla! registration without hacking the core, I want to show you my latest project.
I'm currently working on a series of classifieds components for a non-profit organization which need to work with the normal Joomla! user registration. Since we are in Germany, we need to show the general terms of use and the user has to accept them when he or she registers. The normal Joomla! registration does not provide this, so we are adding this on our own. But how are we going to do this without hacking the core?
Using Template Overrides
First of all, to show the terms of use, we create a template override. The default Joomla! installation already shows how this is done, you simply create a folder named "html" inside your own templates folder and add separate folders for the components that you want to override. Since I like my tableless design, I'm taking the override from the BEEZ template.
That means that I copy the file /templates/beez/html/com_user/register/default.php to the folder /templates/my_template/html/com_user/register/default.php. (This has the little benefit that we can do Joomla! updates without destroying our site with this!) Now we add a little bit of code to this file. Add this below line 45:
<textarea rows="5" cols="50" readonly="readonly">
<?php $language =& JFactory::getLanguage();
$language->load('plg_hpregistration', JPATH_SITE);
$modules =& JModuleHelper::getModules('termsofusage');
if($modules)
{
foreach($modules as $module)
{
echo JModuleHelper::renderModule($module);
}
}
?></textarea><br />
<input type="checkbox" name="acceptedtermsofuse" value="1" />
<?php echo JText::_('I accept the terms of use'); ?><br />
<textarea rows="5" cols="50" readonly="readonly">
<?php
$language =& JFactory::getLanguage();
$language->load('plg_hpregistration', JPATH_SITE);
$modules =& JModuleHelper::getModules('termsofusage');
if($modules)
{
foreach($modules as $module)
{
echo JModuleHelper::renderModule($module);
}
}
?>
</textarea><br />
<input type="checkbox" name="acceptedtermsofuse" value="1" />
<?php echo JText::_('I accept the terms of use'); ?><br />
What does this do? First we create a readonly textfield and inside that text field we are using some PHP code. That PHP code first loads a language file for the additional strings that we are going to use. Thats the two lines that start with $language. Then we load the modules for the position 'termsofusage'. (That's the line starting with $modules.) Now we check if we got any modules in return and if that is the case, we render each and every module that we got inside the textfield. Last but not least, we close the textfield, go to a new line and create a checkbox with a name. This is already 80% of the work done to show our terms of use and make it necessary to accept them.
Building in Terms of Use
But how does this work? I didn't see a terms of use text anywhere? Although I first wanted to have this only for our little non-profit organization, I couldn't use a static text, because I'm no lawyer and the time that it would take until that is waterproof by those that are, would have been to long for me to wait. Last but not least, I will move on some time in the future and if laws change, they would have to change that terms of use, too, which would be nearly impossible, since none of them are coders and could dive into this.
So I need a way for them to edit this with the least amount of difficulties. And here comes the custom HTML module in handy. You still see the code lines above where we retrieve and render the modules for the position "termsofusage". We just have to create a custom HTML module for this position. (That position will not come up in the drop down in the module manager, but you can simply type in "termsofusage" in the positions field.)
Now we have this in the output, but then again, nothing is checking if the user really has accepted the terms. Again, Joomla is very flexible, which means that we just have to create a plugin that reacts upon registration. I wont go into detail on how to create a plugin, it should be enough to say that the class responsible for saving a user fires a trigger called "onBeforeStoreUser", which acts exactly then, just before the user is stored. With the following code we act upon this:
function onBeforeStoreUser($user, $isnew)
{
global $mainframe;
if($isnew && !JRequest::getVar('acceptedtermsofuse', 0))
{
$mainframe->redirect(JRoute::_('index.php?option=com_user&view=register'),
JText::_('You need to accept the terms of use'), 'error');
$mainframe->close();
return false;
}
return true;
}
It's pretty simple, since we only check if the user is new and if the checkbox has not been checked. If that is the case, we directly kill the application and go back to the registration form. And that's basically all there is to it. Now, for those not willing or able to code this on their own, I made a little package, that you can soon find on the Joomla! Extensions Directory and which is available from Joomlacode. This has all the necessary stuff to add this little feature to your site.
Implementing Captcha
Now, I didn't want to just add a terms of use, but since it will be a fairly popular site, I want to protect us against spam bots. Although the standard registration with the eMail activation is already a good way to prevent working accounts, it still creates dummy accounts in the system. Lets prevent this by using a captcha.
I looked at the Joomla! Extensions Directory and found this nice captcha solution: tinCaptcha. It is a simple solution, but then again, do we need much more? I say no and it looks pretty good so far.
When you look into the entry on the JED, you see a forum entry which explains how to use this. Again, we want to implement this without hacking the core of Joomla!. So, after we installed the component and plugin, we add some more code to our own plugin and again to the template override.
In our override, we add this again below line 45:
<img src="/index.php?option=com_tincaptcha&task=captcha_display"
onclick="this.src='index.php?option=com_tincaptcha&task=captcha_display&t='+(new Date()).getTime()"
alt="Click to refresh image"/><br />
<label for="captcha"><?php echo JText::_( 'TIN_CAPTCHA' );?>:</label><br />
<input type="text" name="captcha" id="captcha" size="10" class="inputbox required" value="" /><br />
and in our plugin, we amend this in our function:
function onBeforeStoreUser($user, $isnew)
{
global $mainframe;
// tincaptcha-
$captchk = plgSystemTincaptcha::check(JRequest::getVar('captcha', '', 'post'));
if ($isnew && $captchk !== true)
{
JError::raiseWarning(0, $captchk);
return false;
}
// -tincaptcha
if($isnew && !JRequest::getVar('acceptedtermsofuse', 0))
{
$mainframe->redirect(JRoute::_('index.php?option=com_user&view=register'),
JText::_('You need to accept the terms of use'), 'error');
$mainframe->close();
return false;
}
return true;
}
Voilá, terms of use check and captcha verification in the core Joomla! registration in less than 10 minutes. :)
So, why did I build this extension? I searched for both these features in the Joomla! Extension Directory and I honestly couldn't find a simple solution for this. Especially the terms of use seem to always require you to install Community Builder and even something like a Captcha check requires you to replace the standard Joomla! registration, which does not always work as expected and even might leave open an unwanted door to your site.
I wanted a solution which utilizes the Joomla! core components and that is from the featherweight class and one which does exactly what I want and not an ounce more. In the end, this resulted in a nice coding example on how to extend Joomla!. It does not always have to be the 2 MB super extension. ;)
Hannes Papenberg
Download
If you wish to work with the Terms of Use or Captcha extensions, here are the links:
- HP Registration on Joomla! Extensions Directory
- HP Registration on Joomlacode
- tinCaptcha on the Joomla! Extensions Directory
- tinCaptcha on Joomlacode


2008-12-29 22:29:56
Könnte man das Plugin für die älteren Versionen von Joomla adaptieren?
2009-04-26 01:24:40
very simple and very effective.
Do you know if this affects registration from other components such as virtuemart?
2009-04-30 06:34:00
i am facing problem during implement the tincaptcha
after putting code captcha image is not showing any thing it shows alt test.
any clue why captcha is not showing ?
2009-10-20 02:53:15
thank you..