We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Login PostHooks

  • I am attempting to use pre and post hooks with Login to add a user to a group once they successfully login. I cannot get the hooks to fire. I boiled it down to a simple snippet that just sends me an email and returns true and it won't work. The documentation says the Login snippet can use pre- and post-hooks but it doesn't appear to do so. Anyone else tried something similar? I'm using Revo 2.3.

    This question has been answered by BobRay. See the first response.

      • 3749
      • 24,544 Posts
      I think that should work, but it's probably easier to just do it in a plugin attached to the OnWebLogin event:

      $group = 'SomeUserGroup';
      $role = 'Editor';
      
      if (!$user->isMember($group)) {
          $user->joinGroup($group, $role);
      }


      Paste the code into a plugin, Check OnWebLogin on the System Events tab, save.

      The 'Role' part is optional. IOW, if you don't care about the role, you can use:

          $user->joinGroup($group);


      If you are using the Register snippet as well, once all users have logged in at least once, you could switch the event to OnUserActivate, which will fire when a new user confirms registration.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • Thanks Bob but unfortunately it won't help in this situation. We don't want everyone who logs in to join the group, just those who use this particular form. I submitted a Github issue related to hooks not functioning on the Login snippet (it does on the Register snippet).

        As a last resort I may create a landing page that will add them to the group and redirect them.
        • discuss.answer
          • 3749
          • 24,544 Posts
          You could have the form page set a $_SESSION variable, then check for and (if it exists) clear that variable in the plugin:

          if (! isset($_SESSION['new_user'])) {
              return '';
          } else {
              unset($_SESSION['new_user']);
          }
          
          $group = 'SomeUserGroup';
          $role = 'Editor';
           
          if (!$user->isMember($group)) {
              $user->joinGroup($group, $role);
          }
          

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • Thanks! Using a session combined with a plugin would do the trick. I need to use plugins more often.