Discover with this tutorial how to display the name of the logged in user anywhere on the web thanks to a shortcode.
One of the advantages of being a OsomCode subscriber is that you can make suggestions for new tutorials.
The other day, a colleague made me the following suggestion for a tutorial:
Greetings, Nahuai! One simple question: How to simply greet each subscriber when they log in to the site: Hello, [user-name].
Another use of this would be to personalize the content, for example of courses: “Do you remember [username] how you learned to add? Yes? Well, get ready [username] because today you will learn how to multiply.”
The idea comes from the “newsletter” we receive by email where the emails “seem” to have been written exclusively for us.
Greetings Nahuai and to all subscribers.
An interesting proposal, isn’t it?
If you also want to place personalized messages to your subscriber at certain points of the web, here’s how to do it by combining the
wp_get_current_user() function and WordPress shortcodes.
The end result could look something like this:
Or any combination you can think of.
Steps to display the username using a shortcode
1. Create the shortcode
Add the following code at the end of functions.php or in your functionality plugin:
With this code you create a shortcode that allows you to display the name of the “logged in” user when typing [username].
In addition, you give it the CSS class of “username” in case you want to add some custom styling to the name.
Now there is only one step left.
2. Insert the shortcode where you want
The advantage of shortcodes is that you can insert them anywhere on the website.
You only have to type “[your_shortcode]” where you want it to be displayed, in this case .
- If you are using the classic editor, use the HTML tab to enter the shortcode.
- In case you are already using the new block editor (Gutenberg) you can add it to the normal paragraph block, the HTML block or the shortcode block. All of them will render the content correctly.
You can see how easy it is to show the name of a logged in user anywhere on the web thanks to this custom shortcode.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉