Redirecting Users on Login V

Redirect users on login by checking group membership directly

In the last article, we saw how to redirect users after login based on a user group setting. In this one, we'll see how to do it on the basis of their user group memberships, but we won't use a user group setting, and we'll control the order in which the groups are checked. Since we don't know who the user is until *after* they've logged in, we need to put the code in a plugin connected to the OnWebLogin System Event.

Redirecting Users Based on User Group Membership

In this version, we'll redirect the user after login based on user group membership, but instead of checking a user group setting, will check group membership directly with $user->isMember().

The Plugin Code

Change the default value and change the array of links to match actual groups on your site and the IDs of pages you want their members sent to. The groups will be checked in the order they appear in the array.

/* LoginRedirect plugin */

/* ID to send them to if they don't belong to any of the groups */
$default = 12;
/* Set up the IDs to redirect to for each group; user group name on the left
   ID to redirect to on the right */

$links = array(
    'Editors' => 9,
    'Publishers' => 17,
    'Members' => 14,


$sendTo = $default;
foreach ($links as $groupName => $id) {
    if ($user->isMember($groupName)) {
        $sendTo = $id;

/* Generate the URL */
$url = $modx->makeUrl($id, "", "", "full");

/* Forward the User */

The break; statement is very important. It will short-circuit the foreach() loop at the first group the user is a member of. Without it, user will be sent to the page of the *last* group they are a member of. If you want that, just remove the break; statement.


There are two potential drawbacks to this method. First, you have to edit the plugin code to add new groups or change the page IDs to redirect to, which you may or may not be comfortable doing. Second, you can no longer create a user setting to override the plugin's behavior for individual users. In the next article, we'll use a somewhat more complex version of the plugin to change that.


Comments (0)

Please login to comment.