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

Kişiselleştirilmiş Sonuçlar için WordPress’te Oturum Açmış Kullanıcı Bilgileri Nasıl Alınır?

Kısa bir süre önce, favori gönderilerini kişiselleştirilmiş bir kütüphaneye kaydetmelerini sağlayarak kullanıcılarınız için nasıl kişiselleştirilmiş bir deneyim oluşturabileceğinizi gösterdik. Bazı yerlerde (örneğin karşılama ekranında) kullanıcıların ilk adlarını kullanarak kişiselleştirilmiş sonuçları başka bir seviyeye taşıyabilirsiniz. Neyse ki WordPress, giriş yapan kullanıcının bilgilerini almayı gerçekten kolaylaştırıyor. Bu makalede, o anda oturum açmış olan kullanıcıya ait bilgilerin nasıl alınacağını göstereceğiz.

get_currentuserinfo(); fonksiyonunu kullanacağız. Bu, temanızın herhangi bir yerinde kullanılabilir (üstbilgi, altbilgi, kenar çubuğu, sayfa şablonu vb.) Bunun çalışması için kullanıcının giriş yapmış olması gerekir. Bu yüzden is_user_logged_in() koşullu ifadesini kullanmamız gerekecek. Örnek 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 } ?>

Şimdi oturum açmış kullanıcılar için özel bir mesaj gösterebiliriz, örneğin, “Hey Syed, Her şey burada, tam da olmasını umduğun yerde”. Yukarıdaki kod aşağıdaki gibi bir şeye dönüşecektir:

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

Yukarıda eklediğimiz sihirli kod $current_user->user_firstname; şeklindedir ve get_currentuserinfo() çağrısı mevcut kullanıcının bilgilerini $current_user içine yerleştirdiği için çalışmaktadır. Benzer yöntemi kullanıcı hakkında giriş, kullanıcı kimliği, e-posta, web sitesi gibi diğer bilgileri almak için de kullanabilirsiniz.

İşte tüm bilgilerin örnek bir kullanımı:

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

Umarım bu yardımcı olur. Bunu favori gönderi ekleme özelliği ile birleştirerek kolayca kişiselleştirilmiş bir deneyim oluşturabilirsiniz.

Açıklama: İçeriğimiz okuyucu desteklidir. Bu, bazı bağlantılarımıza tıklarsanız komisyon kazanabileceğimiz anlamına gelir. WPBeginner'ın nasıl finanse edildiğini, neden önemli olduğunu ve nasıl destek olabileceğinizi görün. İşte editoryal sürecimiz.

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.

Ultimate WordPress Araç Kiti

Araç setimize ÜCRETSİZ erişim sağlayın - her profesyonelin sahip olması gereken WordPress ile ilgili ürün ve kaynaklardan oluşan bir koleksiyon!

Reader Interactions

10 yorumBir Cevap Bırakın

  1. 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!

  2. 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.

  3. 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.

  4. 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?

Bir Cevap Bırakın

Yorum bırakmayı seçtiğiniz için teşekkür ederiz. Lütfen tüm yorumların yorum poli̇ti̇kasi uyarınca denetlendiğini ve e-posta adresinizin yayımlanmayacağını unutmayın. Ad alanında anahtar kelime KULLANMAYIN. Kişisel ve anlamlı bir sohbet edelim.