We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27106
    • 147 Posts
    For a non-developer, is there a simple way to test whether a user is in a particular user group? I want to hide a "Subscribe" link from users who are already subscribers ...

    It is easy to do this test for anonymous users:
    [[If? &operator=`EQ` &operand=`(anonymous)` &subject=`[[+modx.user.username]]` &then=`CONTENT HERE`]]


    But the documentation at http://rtfm.modx.com/display/revolution20/Users says that this test can only be applied for usernames and IDs.
      David Walker
      Principal, Shorewalker DMS
      Phone: 03 8899 7790
      Mobile: 0407 133 020
      • 3749
      • 24,544 Posts
      There is an ismember output modifier, but the most efficient way is a simple snippet:

      Put the subscribe link in a chunk called Subscribe.
      Put this where the link goes:

      [[!ShowSubscribeLinkHere]]


      Create a snippet called ShowSubscribeLinkHere with this code:

      <?php
      /* ShowSubscribeLinkHere snippet */
      $output = "";
      
      if (!$modx->user->isMember('Subscribers')) {
      
          $output = $modx->getChunk('Subscribe');
      
      } 
      return $output;
      
        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
        • 27106
        • 147 Posts
        Not working for me yet. Probably my fault.
          David Walker
          Principal, Shorewalker DMS
          Phone: 03 8899 7790
          Mobile: 0407 133 020
          • 3749
          • 24,544 Posts
          Quote from: shorewalker at Jul 09, 2011, 02:29 PM

          Not working for me yet. Probably my fault.

          More likely mine. There was a typo in the code (corrected above). Watch out for case-sensitivity in the snippet and chunk names also.
            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
            • 27106
            • 147 Posts
            Thanks BobRay. I saw the misplaced quote marks and corrected them. I consistently get a 500 Internal Server Error when I place the snippet in the page. Checked and rechecked, and now driving me nuts ...
              David Walker
              Principal, Shorewalker DMS
              Phone: 03 8899 7790
              Mobile: 0407 133 020
              • 4385
              • 372 Posts
              I was having similar problems last night with this bit of code

              <?php
              $output = $modx->user->isMember($group) ? $modx->getChunk($yesChunk) : $modx->getChunk($noChunk);
              return $output;
              ?>


              I found out that it was a problem being logged in as an admin. Try using another browser without being logged into the manager.
                DropboxUploader -- Upload files to a Dropbox account.
                DIG -- Dynamic Image Generator
                gus -- Google URL Shortener
                makeQR -- Uses google chart api to make QR codes.
                MODxTweeter -- Update your twitter status on publish.
                • 27106
                • 147 Posts
                OK, solved it by fixing one more typo. So the code to display info only to non-subscribers, where subscribers are members of the user group "Subscriber", is as follows:
                <?php
                /* ShowSubscribeLinkHere snippet */
                $output = "";
                
                if (!$modx->user->isMember('Subscribers')) {
                
                    $output = $modx->getChunk('Subscribe');
                
                } 
                return $output;


                If you’re building a subscription site, the most obvious use of this is to display content asking non-subscribers to subscribe.

                Big thanks to BobRay for this.
                  David Walker
                  Principal, Shorewalker DMS
                  Phone: 03 8899 7790
                  Mobile: 0407 133 020
                  • 3749
                  • 24,544 Posts
                  Glad it worked (finally). Sorry about the typos.

                  BTW, if you want it to be even faster, you can skip the chunk and put the link text directly in the snippet:

                  <?php
                  /* ShowSubscribeLinkHere snippet */
                  $output = "";
                  
                  if (!$modx->user->isMember('Subscribers')) {
                  
                      $output = '<a href="url/of/subscribe/page">Subscribe</a>';
                  
                  } 
                  return $output;
                    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
                    • 27106
                    • 147 Posts
                    BTW, is the MODx community going to hold some sort of virtual party for BobRay’s 10,000th post? It’s coming up soon, and it seems like it should be honored somehow. I’ve only been involved for a few weeks, but it’s obvious he’s made one hell of a contribution ...

                    Or we could just send money: http://bobsguides.com/support-this-site.html.
                      David Walker
                      Principal, Shorewalker DMS
                      Phone: 03 8899 7790
                      Mobile: 0407 133 020
                      • 24598
                      • 17 Posts
                      I'm buying the book.. The guys a great evangelist for modx...