Welcome/welcome back banner code not working?

I tried this and it not showing the user name.

Can anyone take a look as why it is not working now? :slightly_smiling_face:

I want to use it. :slightly_smiling_face:

Hello @kynic :wave:

Is it works for you?

If name is available it shows the name if only username available it will shows the username.

Just to stay the current example above :arrow_down_small:

Header

<script type="text/discourse-plugin" version="0.8">
  api.onPageChange(() => {
    if (api.getCurrentUser()) {
      const loggedInUser = document.getElementById("logged-in-user");
      if (api.getCurrentUser().name) {
        loggedInUser.innerText = "Hey " + api.getCurrentUser().name + "!";
      } else {
        loggedInUser.innerText = "Hey " + api.getCurrentUser().username + "!";
      }
    }
  });
</script>

After Header

<h2 class="x-title"><span id="logged-in-user"></span> Bienvenue dans ta nouvelle communauté</h2>

I hope this helps. :slight_smile:

4 Likes

Hi,

I was testing this on my local host, and when I added my full name to the user profile, it started to show up. But should it also work without adding a full name?

Anyway, on the production site, it works well.

I want to show welcome back user name to logged in users and welcome to non-logged in users.

Can anyone help?

Amend the Javascript to return “Welcome” if there is no current logged-in user—

<script type="text/discourse-plugin" version="0.8">
  api.onPageChange(() => {
    const loggedInUser = document.getElementById("logged-in-user");
    if (api.getCurrentUser()) {
      if (api.getCurrentUser().name) {
        loggedInUser.innerText = "Hey " + api.getCurrentUser().name + "!";
      } else {
        loggedInUser.innerText = "Hey " + api.getCurrentUser().username + "!";
      }
    } else {
      loggedInUser.innerText = "Welcome"
    }
  });
</script>
3 Likes