Change the Name of the Anonymous User

The right way to modify the displayed username for not-logged-in users on the front end of your site.

By Bob Ray
October 18, 2022
Change the Name of the Anonymous User

A long time ago, the username of not-logged-in users in MODX was hard-coded as (anonymous) (including the parentheses). It’s still the default, but it’s no longer hard-coded.

Some years ago, I wrote a long article about how to use a Snippet or Plugin to change the displayed name of anonymous users in a way that wouldn’t break anything. I also gave some terrible advice, which unfortunately also appears in the first edition of my MODX book (more on the advice in a bit).

An Easier Way

It’s now ridiculously easy to change the displayed username of anonymous users. You just change the default_user System Setting. Once you’ve done that, you can display that username with this tag:

[[+modx.user.username]]

You can also do it with a one-line Snippet:

return $modx->user->get('username');

When Would You Use This?

Suppose that a page is public, and can be seen by anyone whether they’re logged in or not. When a visitor is not logged in, you still want to show them a greeting.

As we said above, one easy way to do this is with the modx.user.username placeholder, which MODX sets automatically for every page:

Hello [[!+modx.user.username]]

Where Does the Username Come From?

Here’s the code in the MODX class that sets the value of the id and username fields of the $modx->user object:

$this->user = $this->newObject('modUser');
$this->user->fromArray(array(
    'id' => 0,
    'username' => $this->getOption('default_username','','(anonymous)',true)
), '', true);

Notice that it uses the System Setting for the username, and it will still set the username to (anonymous) if the System Setting is blank or missing.

Here’s the code that sets the placeholders for the id and username. It uses the $modx->user object set in the code above for the source of the two values (seen below as $this->user, since we’re inside the modX class):

$this->toPlaceholders($this->user->get(array('id','username')),
    'modx.user');

In the code above modx.user is set as the prefix for both the username and the id, so either of these placeholders will work.

[[+modx.user.id]]
[[+modx.user.username]]

What About the Anonymous Username?

Of course you can also display the name of the anonymous user with this tag:

[[++default_username]]

But there’s not much reason to do this, since it would show that anonymous name to all users whether they’re logged-in or not. It could be used as an option in a Snippet or Pugin, but using [[+modx.user.username]] will be faster and easier.

Bad Advice

A long time ago, when I first wrote about this, I gave some very bad advice. Because in those days, the (anonymous) user was hard-coded, and the default_user System Setting didn’t exist, and I didn’t know any better, I suggested that a quick way to tell if a user was logged in was to check the username, like this:

if ($modx->user->get('username') === ('anonymous')) {
    /* User is not logged in */
}

Here’s the official way of checking the user’s login status, which I wasn’t aware of at the time:

if (! $modx->user->hasSessionContext($modx->context->get('key'))) {
    /* User is not logged in */
}

My original method is not only flawed, it’s positively dangerous. If the default_user System Setting has been changed, it identifies not-logged-in users as logged in!

There’s still a method that would be safe, and might be slightly faster:

if ($modx->user->get('id') == 0) {
    /* User is not logged in */
}

Users who are not logged in will always have an id of 0, but I doubt if the difference in speed is significant, so it’s probably best to use the official method.

Unless you change the default_username System, when a user is not logged in, they’ll see a message that says “Hello (anonymous)”. You might want to change that to something more graceful, like “Guest” or “Visitor”.

If you have my book, you might make some notes whenever the old, unsafe method appears. This will be corrected in the second edition of the book, if I ever manage to finish it.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.