Syntax Guide

Article number: 304301

Registration with auto-login

This feature allows a registration form to be added to a page that will create the new user, automatically log them in and allow them to automatically be added to a user field on a post in a single postback. It's handy for occasions where you don't want to put up a barrier to prevent a user taking some kind of action like booking an event or offering feedback but you still want to collect some data about them once they commit to taking that action.

There are two parts to this:

  • User creation/login.
  • Adding logged in user as a value of a custom field in a post, i.e. as an attendee of an event.

Dealing with these in turn

User Creation/Login

Either create a new user group and set its 'allow self-registration' checkbox or choose an existing group and set its 'allow self-registration' checkbox. Make a note of the group ID. This will be the user group that self-registering users get added to. Then create a form on a page with the minimum fields as follows:

<form name="SelfRegForm" method="POST"> <input type="hidden" name="action" value="createuser"/> <input type="hidden" name="groupid" value="1234"/> Email : <input type="text" name="email"/><br/> Password : <input type="text" name="password"/> <input type="submit" name="Register" value="Register"/> </form>

(where 1234 = the group id)

On the front-end, all a user has to do is enter an email address and a password, hit 'register' and they will then instantly create a new account, be logged in and returned to the same page as a logged-in user. Any number of the following optional extra form fields can be added to the registration form to allow more information to be captured when a user registers:

passwordconfirm   (see below)
email
telephone
fax
fullname
address1
address2
town
county
postcode
country

The minimum fields that can be supplied is a groupid together with just either an email or a username (as in the example above), in which case a random password will be generated.

If a passwordconfirm is included then the password and passwordconfirm must match for a user to be created.

It is possible to add the user to multiple groups by including multiple groupid fields but the first groupid field in the form will always be used as the master group. All of the groups must have their 'allow self-subscription' flag set in the Group Editor in admin. e.g.

<form name="SelfRegForm" method="POST"> <input type="hidden" name="action" value="createuser"/> <!-- Master Group --> <input type="hidden" name="groupid" value="1234"/> <!-- Another Group --> <input type="hidden" name="groupid" value="123"/> <!-- Optional Other Group --> <select name="groupid"> <option value="2345">People who like cats</option> <option value="2346">People who like dogs</option> </select>
Email : <input type="text" name="email"/><br/> Password : <input type="text" name="password"/> <input type="submit" name="Register" value="Register"/> </form>

new magic word called :::errormessage::: allows any error message associated with the registration attempt to be displayed. e.g.

:::errormessage::: <form name="SelfRegForm" method="POST"> <input type="hidden" name="action" value="createuser"/> <input type="hidden" name="groupid" value="1234"/> Email : <input type="text" name="email"/><br/> Password : <input type="text" name="password"/> <input type="submit" name="Register" value="Register"/> </form>

The form can be removed if registration is successful by wrapping it in a sitekit:if using another new magic word that returns true or false if logged in like this:

<sitekit:if op1=":::loggedin:::" operator="=" op2="false"> :::errormessage::: <form name="selfregform" method="POST"> <input type="hidden" name="action" value="createuser"/> <input type="hidden" name="groupid" value="4248"/> Username : <input type="text" name="username"/> <input type="submit" name="Register" value="Register"/> </form> </sitekit:if>

 

Automatic Adding As An Attendee 

Some additional parameters can be added to the same form that will allow the user's people ID to be automatically added to a custom field on a post, e.g.

<sitekit:if op1=":::loggedin:::" operator="=" op2="false">  ::: errormessage::: <form name="selfregform" method="POST"> <!-- Make the new user --> <input type="hidden" name="action" value="createuser"/> <input type="hidden" name="groupid" value="1234"/> Email : <input type="text" name="email"/><br/> Password : <input type="text" name="password"/> <input type="submit" name="Register" value="Register"/> <!-- Add new user as an attendee on a post --> <input type="hidden" name="action" value="addpostfieldvalue"/> <input type="hidden" name="updatepostid" value="5374"/> <input type="hidden" name="postfieldname" value="Attendees"/> <input type="hidden" name="postfieldvalue" value="me"/> </form> </sitekit:if>

Note the second 'action' field and the postfieldvalue of 'me' is translated to be the peopleid of the logged in user. You must use 'me' here, as the :::peopleid::: magic word has no value until the form is submitted and the user is created.

Related questions