Renaming the MODX (anonymous) User

Now there's a much easier way to change the username of the (anonymous) user

A few years back, I wrote an article about changing the name of the anonymous user to something other than (anonymous). The article showed ways to alter the name in case you didn't want anonymous users to be greeted with the phrase "Hello (anonymous)" in the front end of your web site. By the way, in case you're wondering about the parentheses, they are actually part of the username for the anonymous user.

In that older article, the name was changed with a plugin attached to OnWebPagePrerender and a snippet to be placed in the page Template. There's now a much easier way to do this.

MODX logo

A Much Easier Solution

There's now a System Setting with the key default_username. MODX uses its value for the name of not-logged-in users. All you have to do is change that value to "Guest" or whatever you like and "(anonymous)" will never appear on your site again. You can also display the anonymous username on a page with this tag (though I can't think of any reason to do it):


A Word of Warning

It's unlikely, but there could be code on your site that uses the username to determine if a user is logged in or not. I think in a weak moment a long time ago, I may have even recommended this because it's much faster than the "Official" way.

It was significantly faster and clearer to do this:

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

Than this:

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

I've never checked the username to establish login status in any of my extras, and I doubt if anyone else has done this, but it's a possibility. If you change the default_username System Setting value and not-logged-in users suddenly start getting treated as if they are logged in, you'll know what to look for. Any code that looks like the first example above can easily be converted to match the second example.

Coming Up

In the next few articles, we'll look at the issue of updating Manager pages in a plugin.

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.