Changing the Name of the (Anonymous) User in MODX

It’s common to have a greeting somewhere on your pages that looks something like this:

<p>Hello [[!+modx.user.username]]</p>

The problem with this is that when a user is not logged in, they’ll see a message that says “Hello (anonymous).” In this article, we’ll look at some safe ways to change that.

MODX logo

Where Does it Come From?

The modx.user.username placeholder is set in the MODX class file:

$this->user = $this->newObject('modUser');
$this->user->fromArray(array(
    'id' => 0,
    'username' => '(anonymous)'
), '', true);
/* ... */
$this->toPlaceholders($this->user->get(array('id',
    'username')), 'modx.user');

You could just edit the class file and change the name from ‘(anonymous)’ to something else, but that would be a bad idea. First of all, your change would be overwritten every time you upgraded MODX. Second, checking the username is a very fast way of telling if a user is logged in. You may have extras that depend on that username that could break if it’s changed. There may also be code in the MODX core that expects to see that username for not-logged-in users.

Alternatives

A safer method is to change the value with either a plugin attached to the OnWebPagePrerender System Event, or a custom snippet. The following code uses a plugin to simply rewrite the value in any page content before the page is displayed:

$output = &$modx->resource->_output;
$output = str_replace('(anonymous)','Guest',$output);

To put this into action, just create a plugin with the code above, and on the System Event tab, check the OnWebPagePrerender event before saving the plugin.

Of course the code above will change *all* instances of ‘(anonymous)’ to ‘Guest’ but it’s unlikely that you’ll have a lot of those in your web pages. A simple custom snippet, though, will avoid that problem and should be a little faster as well. This snippet is an alternative to the plugin above. While the plugin will execute for every page on the site, the snippet can be placed in just the templates where you need it. It will be faster, and there is no risk of it changing anything you don’t want changed.

Put the following tag where you want to display the user name:

[[!ShowUsername]]

Next, create a snippet called “ShowUsername” with the following code:

/* ShowUsername snippet */
$output = "";
if ($modx->user->get('id') == 0) {
    $output = 'Guest';
} else {
    $output = $modx->user->get('username');
}
return $output;

Since the (anonymous) user’s ID is always 0, the snippet will return ‘Guest’ for anyone who is not logged in. Here’s a much more compact version of the same snippet:

return ($modx->user->get('id') == 0) ? 'Guest' : $modx->user->get('username');

More Options

Here’s a slightly more complicated (and a little slower) version that gives you the option of showing the full name of logged-in users:

[[!ShowUsername? &fullName=`1`]]
/* ShowUsername snippet */
$output = "";
$fullName = $modx->getOption('fullName', $scriptProperties, false);
if ($modx->user->get('id') == 0) {
    $output = 'Guest';
} else {
    if ($fullName) {
        $profile = $modx->user->getOne('Profile');
        if ($profile) {
            $output = $profile->get('fullname');
        }
    } else {
        $output = $modx->user->get('username');
    }
}
if (empty($output)) {
    $output = $modx->user->get('username');
}
return $output;

 


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 quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray

I am the author of MODX: The Official Guide and over 30 MODX add-on components. I host Bob's Guides, a source of valuable information for MODX users, and I've been very active in the MODX Forums with over 18,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 − = 6

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>