Somewhere along the line, you may find it necessary to greet visitors in a different way, based on conditions i.e. Are they logged in or not?. So, let’s take a look at this in code.
Greeting a logged in user:
<!--?php $user = wp_get_current_user(); # retrieving the current user # note: wp_get_current_user() returns a WP_User object echo 'Howdy, ' . $user->display_name . '!' # displaying the display name of the current user ?-->
Conditional Greeting:
<!--?php if ( is_user_logged_in() ) { $user = wp_get_current_user(); echo 'Howdy, ' . $user->display_name . '! Thanks for logging in!'; } else { echo 'Howdy, Guest! We haven\'t seen you around these parts before!'; } ?-->
Another use for wp_get_current_user():
<!--?php $user = wp_get_current_user(); echo 'In case you forgot your registered email address, here it is: ' . $user->user_email; ?-->
I hope you found this resource useful!
Interested in WordPress Plugin Development?
At ZENVA we have a comprehensive video course on WordPress Plugin Development, check it out here.