# How to get username and make an URL with that?

**URL:** https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267
**Category:** Support
**Created:** [7월 27, 2018, 5:35오전 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267 "2018-07-27T05:35:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)
#### Post date: [7월 27, 2018, 5:35오전 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/1 "2018-07-27T05:35:52Z")

</div>

Hi,

One of my project, i use Wordpress and Discourse. I want to create a link for Wordpress user profiles. I connected login system with SSO between Wordpress and Discourse. So i want to create a dynamic link like that: `site.com/members/users/discourseusername`

There are two conditions here.

If user is not logged in link must seems like that:  
`site.com`

If user logged in, link must seems like that:  
`site.com/members/users/discourseusername`

And idea?

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [7월 27, 2018, 5:58오전 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/2 "2018-07-27T05:58:09Z")

</div>

You can try something like this:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
  const loggedIn = Discourse.User.current();

  if (loggedIn != null) {
    var username = loggedIn.username,
      link = "site.com/members/users/" + username;
  } else {
    var link = "site.com";
  }
</script>

```

This gives you `link` as a variable that you can use as the `href` of a dynamic link.

---

<div class="post-metadata">

### Author: ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)
#### Post date: [7월 27, 2018, 9:03오전 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/3 "2018-07-27T09:03:49Z")

</div>

Thanks @Johani I want to use that link for logo. How can we adapt?

How can i use that in a href tag?

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [7월 27, 2018, 12:46오후 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/4 "2018-07-27T12:46:21Z")

</div>

Sure, you can do that by adding

```plaintext
api.changeWidgetSetting('home-logo', 'href', link);

```

just before the close tag for the script like so:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
  const loggedIn = Discourse.User.current();

  if (loggedIn != null) {
    var username = loggedIn.username,
      link = "//site.com/members/users/" + username;
  } else {
    var link = "//site.com";
  }

  api.changeWidgetSetting('home-logo', 'href', link);
</script>

```

Do note however that this is probably **not a good idea.**

You’re better off adding the link to the header as opposed to changing the logo. On every website I visit, my expectation is that clicking the logo will either take you to the homepage or to the top of the current page. I would find the change you’re trying to accomplish a bit annoying.

Try something like this instead:

```plaintext
<script type="text/discourse-plugin" version="0.8.18">
  const loggedIn = Discourse.User.current();

  if (loggedIn != null) {
    var username = loggedIn.username,
      link = "//site.com/members/users/" + username;
  } else {
    var link = "//site.com";
  }

  api.decorateWidget('header-icons:before', helper => {
      return helper.h('li.wordpress-profile', [
          helper.h('a.icon.btn-flat', {
              href: link, 
              title: 'Your wordpress profile', // change link title
              target: '_blank' // opens in a new tab
          }, helper.h('i.fa.fa-user.d-icon')), // change link icon
      ]);
  });
</script>

```

to add an icon with the dynamic link in the header like so:

 ![Capture](https://global.discourse-cdn.com/meta/original/3X/8/2/826fb287e7b8b23c99a916825f128fb1012c34b8.PNG)

You can then hide that icon for non-logged in users if you want with:

```plaintext
.anon .wordpress-profile {
  display: none;
}

```

or you can keep it, depending on what icon you choose.

---

<div class="post-metadata">

### Author: ![bekircem](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bekircem/32/44582_2.png) [@bekircem](https://meta.discourse.org/u/bekircem)
#### Post date: [7월 27, 2018, 12:54오후 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/5 "2018-07-27T12:54:44Z")

</div>

> [@Johani](#):
>
> Do note however that this is probably **not a good idea.**
> 
> You’re better off adding the link to the header as opposed to changing the logo. On every website I visit, my expectation is that clicking the logo will either take you to the homepage or to the top of the current page. I would find the change you’re trying to accomplish a bit annoying.

I totally agree with you about that. But it’s not my own project and project’s owner wanted it.

> [@Johani](#):
>
> to add an icon with the dynamic link in the header like so:
> 
> [![Capture](https://global.discourse-cdn.com/meta/original/3X/8/2/827c955c68ffc2e2583520e929e5646179fdcb8d.png)  
> Capture.PNG1765x119 20.1 KB  
> ](https://global.discourse-cdn.com/meta/original/3X/8/2/826fb287e7b8b23c99a916825f128fb1012c34b8.PNG)
> 
> You can then hide that icon for non-logged in users if you want with:
> 
> ```plaintext
> .anon .wordpress-profile {
> display: none;
> }
> 
> ```
> 
> or you can keep it, depending on what icon you choose.

That’s a really good idea. And it works great.

You explained everything. Thank you for your help! 🙂

By the way, how i can learn Discourse API terms like “Discourse.User.current();” and “api.decorateWidget”? Could you suggest a resource?

---

<div class="post-metadata">

### Author: ![Johani](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/johani/32/176920_2.png) [@Johani](https://meta.discourse.org/u/Johani)
#### Post date: [7월 28, 2018, 2:17오전 UTC](https://meta.discourse.org/t/how-to-get-username-and-make-an-url-with-that/93267/6 "2018-07-28T02:17:00Z")

</div>

> [@bekircem](#):
>
> By the way, how i can learn Discourse API terms like “Discourse.User.current();” and “api.decorateWidget”? Could you suggest a resource?

For now you can have a look at some of the pluginAPI examples [here](https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/lib/plugin-api.js.es6).

However, we also have more structured theme developer documentation coming out very soon. 👍
