Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
Puchar WPB
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

Jak uzyskać informacje o zalogowanym użytkowniku w WordPress dla spersonalizowanych wyników

Niedawno pokazaliśmy, jak stworzyć spersonalizowane doświadczenie dla twoich użytkowników, umożliwiając im zapisywanie ulubionych wpisów w spersonalizowanej bibliotece. Możesz przenieść spersonalizowane wyniki na inny poziom, używając ich imienia w niektórych miejscach (np. na ekranie powitalnym). Na szczęście WordPress sprawia, że uzyskanie informacji o zalogowanym użytkowniku jest naprawdę łatwe. W tym artykule pokażemy, jak pobrać informacje dotyczące obecnego, aktualnego użytkownika.

Wykorzystamy do tego funkcję get_currentuserinfo();. Może być ona użyta w dowolnym miejscu twojego motywu (nagłówek, stopka, panel boczny, szablon strony itp.). Aby to zadziałało, użytkownik musi się logować. Będziemy więc musieli użyć instrukcji warunkowej is_user_logged_in(). Przykładowy 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 } ?>

.

Teraz dla zalogowanych klientów możemy wyświetlić własną wiadomość, na przykład „Hej Syed, Wszystko jest tutaj, dokładnie tam, gdzie miałeś nadzieję, że będzie”. Powyższy kod zmieni się w coś takiego:

<?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 } ?>

.

Magiczny kod, który dodaliśmy powyżej to $current_user->user_firstname ; który działa, ponieważ wywołanie get_currentuserinfo() umieszcza informacje o obecnym użytkowniku w $current_user. Możesz użyć podobnej metody, aby uzyskać inne informacje o użytkowniku, takie jak jego login, identyfikator użytkownika, e-mail, witryna internetowa itp.

Oto przykładowe użycie wszystkich informacji:

<?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 . "
";
?>

.

Mam nadzieję, że to pomoże. Łącząc to z możliwością dodawania ulubionych wpisów, można łatwo stworzyć spersonalizowane doświadczenie.

Ujawnienie: Nasze treści są wspierane przez czytelników. Oznacza to, że jeśli klikniesz na niektóre z naszych linków, możemy otrzymać prowizję. Zobacz jak WPBeginner jest finansowany, dlaczego to ma znaczenie i jak możesz nas wspierać. Oto nasz proces redakcyjny.

Avatar

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

Najlepszy zestaw narzędzi WordPress

Uzyskaj BEZPŁATNY dostęp do naszego zestawu narzędzi - zbiór produktów i zasobów związanych z WordPressem, które każdy profesjonalista powinien mieć!

Reader Interactions

10 komentarzyZostaw odpowiedź

  1. Syed Balkhi

    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!

  2. Stef

    You should update this. It’s outdated/deprecated.

    • WPBeginner Support

      Thank you for your feedback and for letting us know about this article :)

      Administrator

  3. Hash

    How to display logged in user’s profile picture ?

  4. S Meredith

    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.

  5. ajay

    how can i echo user infomation in page

  6. Pavan

    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.

  7. Faruk

    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?

Zostaw odpowiedź

Dziękujemy za pozostawienie komentarza. Pamiętaj, że wszystkie komentarze są moderowane zgodnie z naszymi polityka komentarzy, a Twój adres e-mail NIE zostanie opublikowany. NIE używaj słów kluczowych w polu nazwy. Przeprowadźmy osobistą i konstruktywną rozmowę.