Use Full Name in Registration Email I

A trick that lets you put the user's full name in your Registration email

This trick is in response to a MODX Forum question by SyberKnight. It allows you to use the user's full name in the registration email. To be honest, I didn't think it would work, but it does. There may be an easier solution, but I wasn't able to find one.

MODX logo

The Problem

When a user registers, you can ask them for their full name (in addition to their username) but the Register snippet and its processor have only the $user object to work with, and the modUser and modUserProfile objects haven't been created yet in the database. The $user object holds only the username and password. The full name gets stored later in the fullname field of the User Profile, so the Register snippet has no direct way to get it.

The Solution

The way out of this, is to use the fact that when any placeholder is set, and there is a matching tag in the emailTpl chunk, Register will replace the tag with the placeholder value. The trick, then, is to add a preHook to the Register snippet that sets a placeholder for the fullname and add a placeholder for it to the emailTpl chunk. Despite the name, preHooks run after the database has been updated, and because preHooks will run before postHooks, the placeholder will be set by the time the email postHook runs.

Because we can get the username in our preHook, we can use that information to get the modUserobject. Once we have that, we can get the user's modUserProfile object. From there, it's an easy step to set the placeholder with the value of the User Profile's fullname field.

The Placeholder

Here is the placeholder to put in the emailTpl chunk:


The Property

Add this property to the Register snippet tag:


The FullName preHook

A preHook (like a postHook) is just a snippet. When Register processes each element of the &preHook property, it looks for a snippet with that name and simply runs it. All we need to do is create a snippet called FullName with this code:

/* FullName Snippet */

/* Get the username field in case it's not username */
$usernameField = $this->modx->getOption('usernameField', $scriptProperties, 'username');

/* Get the actual username */
$username = $hook->getValue($usernameField);

/* Get the user object for the registering user */
$user = $modx->getObject('modUser', array('username' => $username));

if ($user) {
    /* Get the user ID */
    $userId = $user->get('id');

    /* Get the User's Profile object */
    $profile = $modx->getObject('modUserProfile', array('internalKey' => $userId));
    if ($profile) {
        /* Get the user's fullname */
        $fullName = $profile->get('fullname');
        $modx->setPlaceholder('fullname', $fullName);
return true;

The code is fairly straightforward. First, we get the usernameField property in case the developer has chosen to use another field for the username (e.g., email) or chose to disguise the name to fool bots. Then, we get the user with that username.

Next, we get the useer's profile. The internalKey field of the modUserProfile object always holds the user's ID, so we use that as our selection criterion (the second argument to getObject().

It's a common error to use the modUserProfile's id field to get a user's profile. It almost always works, but the id field is not guaranteed to hold the user's ID, so using this method can fail and get you the wrong profile or no profile at all. The internalKey field will always hold the User ID of the user whose profile it is.

Once we have the profile, it's a simple step to get the user's full name and set the placeholder so it will be available in the email postHook..

Sanity Checks

Notice our two if statements: if($user) and if($profile). Without them, the following get() statements would throw a PHP error because they are being called on a non-object. The user could be left looking at an unsightly error message or a blank page. The odds of this happening are very small, since Register does extensive validation on the user inputs and would likely bail out before the preHook runs if the user couldn't be created, but it only takes a few milliseconds to check and it's always better to play it safe.

Coming Up

What if the user didn't enter a full name? In the next article, we'll add a little code to fall back to the username if the full name is empty.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.