Unable to get a Discourse User Name for a dynamic path


#1

I just upgraded our Discourse install today and some of our custom menu items were broken. For example, we have a menu item that will take a person to their preferences page (yes, I know you can hit the preference icon under your avatar). From the version we upgraded from to the current version, the URL pathing changed. I’ve tried to fix it up with some java script, but it’s not working (very novice) It returns “undefined” like I’m not logged in, but I am logged in. I’m very novice on javascript and where it should go. I’ve made the modification in Themes/Edit CSS/HTML/Desktop/Header. I’ve put the code snippet I’m using below.

   <li class='last'><a id="pprefs" href='https://SITE_BASE_URL'><span>Preferences</span></a></li>
          <script type="text/javascript">
              <!-- Update menu URLs based on current user -->
              <!-- var currentusername = Discourse.User.currentProp('username'); -->
               var currentusername = Discourse.User.current();
               document.getElementById("pprefs").href = "https://SITE_BASE_URL/u/" + currentusername + "/preferences/account";
          </script>

I have been able to get it to return the path string minus the variable part. So I know it’s calling the script.
I’ve tried a number of different ways (as you can see from the code snippet) Any help in correcting this, telling me where to move the JS to, how to do it correctly (with examples), or that it won’t work (and why) would be greatly appreciated. I’m on code base 393c060 if that helps.


(David Taylor) #2

If all you want to do is link to the current user’s preferences, you could simply use a link like

https://meta.discourse.org/my/preferences

That would avoid JavaScript altogether :slight_smile:


#3

I’m good with that. I’ll give it a try. Thank you! It worked :slight_smile: