Is there a recommended way to get the current username from a post?

AI-generated summary

Getting the Current Username in a Post

Nacho_Caballero wants to create a post with a dynamic URL that includes the currently logged-in user’s username. pfaffman suggests using a theme component to access the post record’s username, referencing the Developer’s guide to Discourse Themes.

However, Nacho_Caballero clarifies that he wants to query the currently logged-in user’s username in real-time, similar to the /my route. pfaffman confirms that this is possible by accessing current_user, with examples available in the developer’s guide.

I was wondering if it’s possible to create a post that references the username of whoever is logged in? Sort of like the /my route but for the frontend.

My use case would be to customize a URL pointing outside of Discourse so when a user clicks it, it will have the query ?username=MY_USERNAME appended to it. The same approach could be used to embed an iframe with that URL, for example.

If this is not possible out of the box, I assume the other alternative would be to write a custom plugin.

Yes. The post record has the username in it (see https://meta.discourse.org/t/is-there-a-recommended-way-to-get-the-current-username-from-a-post/178288.json).

You can create a theme component that would add your link as a plugin outlet. See Developer’s guide to Discourse Themes for some hints.

Thanks, Jay. I definitely need to investigate that option, but I think I didn’t explain myself properly. Here’s the reworded version:

How to create a post that contains a URL that updates based on who clicks on it?

Is there a way I can query the currently logged in user (and its username)? Sort of like the /my route but for the frontend.

Oh, well, the particulars are different but the answer is the same. There should be examples in the developer’s guide, so you can see how to access current_user.