The $user API variable

The $user API variable is your connection to the current user viewing the page.

Unless a user is logged in, $user is always referring to the guest user account. If the user is logged in, then $user can be the superuser or any other user that you may have added to the system.

Technically $user is just a Page object (like $page), except that it contains fields and methods specific to a user. See the full $user API reference for full details on the available methods and properties. Note that $user is represented by the template named user, which can be viewed in Setup > Templates, after adjusting the filters to show system templates.

Examples

Depending on the context, you may have to call $user->of(false); to turn off output formatting before assigning values to a user.

Changing the user's password

<?php
$user->pass = "Bam!";
$user->save(); 

Adding a new user with the "superuser" role

<?php
$u = $users->add('gonzo');
$u->pass = "BamBam!";
$u->addRole("superuser");
$u->save();

Latest news

  • ProcessWire Weekly #519
    In the 519th issue of ProcessWire Weekly we'll check out a new third party module called RockForms, introduce the latest ProcessWire core updates, and more. Read on!
    Weekly.pw / 20 April 2024
  • ProFields Table Field with Actions support
    This week we have some updates for the ProFields table field (FieldtypeTable). These updates are primarily focused on adding new tools for the editor to facilitate input and management of content in a table field.
    Blog / 12 April 2024
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK