Use Full Name in Registration Email II

Fall back to the username if the fullname field is empty

In the last article, we looked at a method to use the user's full name in the Registration email. In this one, we'll add a little code to fall back to the username if the fullname field is empty.

MODX logo

The Code

The &preHook property and the placeholder in the emailTpl chunk don't need to change. We just need to add a little code to our FullName snippet to check to see if the fullname field is empty. If it is, we get the username and use that for our placeholder value.

/* 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');

        /* Fall back to the username if fullname is empty */
        if (empty($fullName)) {
            $fullName = $user->get('username');
        $modx->setPlaceholder('fullname', $fullName);
return true;

Now, the user can never get an email that starts dear: (nothing here)

Coming Up

In the next few articles, we'll look at Weblinks — what they are, and how and when to use them.

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.