Nyligen visade vi you hur du skapar en personlig upplevelse för dina användare genom att allow them save their favorite posts in a personalized library. You can take personalized results to another level by using their first name at places (i.e. the welcome screen). Lyckligtvis gör WordPress det väldigt enkelt att få information om den inloggade användaren. I den här artikeln visar vi hur du hämtar information som rör den användare som för närvarande är inloggad.
Vi kommer att använda funktionen get_currentuserinfo();. Denna kan användas var som helst i ditt theme (header, footer, sidebar, page-template etc.). För att detta ska fungera måste användaren vara inloggad. Så vi måste använda det villkorliga uttalandet is_user_logged_in(). Exempel på kod:
<?php if ( is_user_logged_in() ) { ?> <!-- text that logged in users will see --> <?php } else { ?> <!-- here is a paragraph that is shown to anyone not logged in --> <p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p> <?php } ?>
Nu för de inloggade användarna kan vi visa ett custom message till exempel, ”Hey Syed, Everything is here, right where you hoped it would be”. Ovanstående kod kommer att förvandlas till något som gillar detta:
<?php if ( is_user_logged_in() ) { ?> <!-- text that logged in users will see --> <?php global $current_user; get_currentuserinfo(); ?> <h1>Hi <?php echo $current_user->user_firstname; ?></h1> <p>Everything is here, right where you hoped it would be :)</p> <?php } else { ?> <!-- here is a paragraph that is shown to anyone not logged in --> <p>By <a href="<?php bloginfo('url'); ?>/wp-register.php">registering</a>, you can save your favorite posts for future reference.</p> <?php } ?>
Den magiska koden som vi addade ovan är $current_user->user_firstname; vilket fungerar eftersom anropet till get_currentuserinfo() placerar den aktuella användarens information i $current_user. You can use the similar method to get other information about the user such as their login, user ID, email, website etc.
Här är ett exempel på användning av all information:
<?php global $current_user; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . " "; echo 'User email: ' . $current_user->user_email . " "; echo 'User first name: ' . $current_user->user_firstname . " "; echo 'User last name: ' . $current_user->user_lastname . " "; echo 'User display name: ' . $current_user->display_name . " "; echo 'User ID: ' . $current_user->ID . " "; ?>
Hoppas att detta hjälper. Genom att kombinera detta med möjligheten att add to favorit posts kan du enkelt skapa en personlig upplevelse.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Stef says
You should update this. It’s outdated/deprecated.
WPBeginner Support says
Thank you for your feedback and for letting us know about this article
Administratör
Hash says
How to display logged in user’s profile picture ?
S Meredith says
Hi Syed,
Fantastic site.
It’s helped me a lot with understanding WP and editing my own site.
My question is, once I’ve inserted the above function into my child themes functions.php, how would i create a shortcode so that I can then display this user information on a generic page?
Are shortcodes the best way to allow this to be displayed on any page i’d like.
For example, I have a static page that I would like to display ’Welcome {user_name}’ and their profile picture.
WPBeginner Support says
Shortcodes are easier way to do that. You can also create templates with specific WordPress functions. See our guide on how how to add a shortcode in WordPress
Administratör
ajay says
how can i echo user infomation in page
Pavan says
I know nothing about php. I just want to show the username of my customer on TOP bar navgation, and from there he can access his account. How do I do that.
Faruk says
What if the user not logged in. Can the wordpress still send personalised email with his name? and if sa what is the variable and how?
WPBeginner Support says
No WordPress can’t do that.
Administratör