We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Was working on a site today when Scotty D asked "how do I put a welcome back message withe the username ..." so I threw the following together. Now I’m using it on a site I’m working on for a club membership:

    #::::::::::::::::::::::::::::::::::::::::
    # Snippet name: Personalize 
    # Short Desc: basic personalization for logged in users
    # Version: 1.0
    # Created By: Ryan Thrash (vertexworks.com)
    #
    # Date: December 1, 2005
    #
    # Changelog: 
    # Dec 1, 05 -- initial release
    #
    #::::::::::::::::::::::::::::::::::::::::
    # Description: 	
    #	Checks to see if users belong to a certain group and 
    #	displays the specified chunk if they do. Performs several
    #	sanity checks and allows to be used multiple times on a page.
    #	Only meant to be used once per page.
    #
    # Params:
    #	&message [string] (optional)
    #		simple message to prepend in front of the username
    #
    #	&wrapper [string] (optional) 
    #		optional element to wrap the message in
    #
    #	&class [string] (optional) 
    #		optional name of the class for the wrapper element
    #
    #	&ph [boolean] ( optional ) 
    #		if set, outputs to the ph name passed in, instead 
    #		of directly returning the output
    #
    # Example Usage:
    #
    #	[[Personalize? &message=`Welcome back, ` &wrapper=`h3` &class=`welcome`]]
    #
    #	For a logged in user John, would return: 
    #	<h3 class="welcome">Welcome back, John</h3>
    #
    #::::::::::::::::::::::::::::::::::::::::
    
    # is there a class defined?
    $class = (isset($class))? ' class="'.$class.'"' : '';
    
    # build the wrappers as needed
    if (isset($wrapper)) {
    	$w1 = '<'.$wrapper.$class.'>' ;
    	$w2 = '</'.$wrapper.'>';
    } else {
    	$w1 = '';
    	$w2 = '';
    }
    
    # add in the message
    $message = (isset($message))? $message : '';
    
    # do the work
    $o = '';
    $test = $modx->getLoginUserName();
    $o = ($test)? "$w1$message$test$w2" : '';
    
    if (isset($ph)) {
    	$modx->setPlaceholder($ph,$o);
    } else {
    	return $o;
    }


    (I’m still not a coder...)
      Ryan Thrash, MODX Co-Founder
      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
      • 26435
      • 1,193 Posts
      The original one you worked out this afternoon on AIM was terrific, it did the job... but the options you added make this snippet oh so very, very nice. As an example of usage, on a blog site I did, when the user logs in they are directed to the page "account-settings.html.

      [[Personalize? &message=`Welcome  ` &wrapper=`h3` &class=`welcome`]]<br />
      You are now logged in to [(site_name)]<br />
      You have been granted full access to post comments on the <a href="[~2~]">blogs</a>.<br /><br />
      <h3>Change Your Password: </h3>
      [[WebChangePwd]]<br /><br />
      <h3>Advanced Search:</h3>
      [!FlexSearchForm? &FSF_landing=`8`!] <br /><br />
      <h3>Relinquish all your rights:</h3>
      <a href="[~4~]?webloginmode=lo">Log Out</a>.
      
      


      You may need to change the [~ID~] calls to suite your site, but these calls match the standard MODx install.


      I also made a new "Document Group" called "userpages" and set the permissions on account-settings.html to include this group. That way, when a user logs in, the Account Settings page is available to them at all times in the main menu. When a visitor is not logged in, it is invisible.

      -sD-
        Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
        All of the above... in no specific order.


        I send pointless little messages
      • Why not use a user-specified chunk as a template instead of using arguments? It would be a good exercise in using placeholders!
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
        • That’s why I’m not a coder, remember!?! wink

          But I’m willing to try I suppose. smiley
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • Actually, looking at it, it IS a placeholder (if you want it to be)! That’s what I get for being too quick to throw in my two cents at 3:30 AM!
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 26435
              • 1,193 Posts
              Susan, how do you call it as a placeholder?
              I tried [+uid+] (as it is in the system config `user settings --> signup email) but, alas, there was no joy to be had in that huh

              I would much rather call it using a [+placeholder+]. Is there a `system wide` placeholder for regurgitating the users screen name?
                Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
                All of the above... in no specific order.


                I send pointless little messages
              • I’ll check it out; it should be enough to put [!Personalize?ph=`uid`!] somewhere in the page or the template, then in a chunk (or document or template...] use [+uid+].

                Something just occurred to me, it would be interesting to have it return "guest" or "visitor" or "anonymous coward" or something if the user isn’t logged in. Maybe that could be an optional argument, since it wouldn’t always be appropriate.

                [!Personalize?ph=`uid`&guest=`visitor`!]

                It would put whatever value you gave the argument.

                Works like a charm! (http://www.sottwell.com) It’s in the Welcome ! message, and the header for the login block! One snippet stuck in the template, then the placeholder in the Welcome document and the LoginBlock chunk.

                Now to get it to display a different $message if the user is or is not logged in!
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                • This is beginning to stir up ideas for improving the Checkout section of shopX. Hmmmm...
                    Studying MODX in the desert - http://sottwell.com
                    Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                    Join the Slack Community - http://modx.org
                  • I figured it might spawn a few ideas... Replace the check with something else and run with it. Makes for a more building-block type of possibilities, wouldn’t you say... wink
                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 33337
                      • 3,975 Posts
                      Another nice snippet !

                      Thank you very much Ryan smiley

                      regards,

                      zi
                        Zaigham R - MODX Professional | Skype | Email | Twitter

                        Digging the interwebs for #MODX gems and bringing it to you. modx.link