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

Comment obtenir les informations de l’utilisateur connecté dans WordPress pour des résultats personnalisés

Nous vous avons récemment affiché comment créer une expérience personnalisée pour vos utilisateurs/utilisatrices en leur permettant d’enregistrer leurs publications préférées dans une bibliothèque personnalisée. Vous pouvez pousser la personnalisation des résultats à un autre niveau en utilisant leur prénom à certains endroits (par exemple l’écran d’accueil). Heureusement, WordPress permet d’obtenir très facilement les informations de l’utilisateur/utilisatrice connecté(e). Dans cet article, nous allons vous afficher comment récupérer les informations relatives à l’utilisateur connecté.

Nous utiliserons la fonction get_currentuserinfo() ;. Cette fonction peut être utilisée n’importe où dans votre thème (en-tête, pied de page, colonne latérale, modèle de page, etc.) Afin de commander cette fonction, l’utilisateur doit être connecté. Nous devons donc utiliser l’instruction conditionnelle is_user_logged_in(). Exemple de code :

1
2
3
4
5
6
7
<?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 } ?>

Pour les utilisateurs/utilisatrices connectés, nous pouvons afficher un message personnalisé, par exemple « Hey Syed, Everything is here, right where you hoped it would be » (Hé Syed, tout est là, à l’endroit où tu l’espérais). Le code ci-dessus se transformera en quelque chose comme ceci :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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 } ?>

Le code magique que nous avons ajouté ci-dessus est $current_user->user_firstname ; ce qui fonctionne parce que l’appel à get_currentuserinfo() place les informations de l’utilisateur actuel dans $current_user. Vous pouvez utiliser la même méthode pour obtenir d’autres informations sur le compte, comme sa connexion, son ID utilisateur, son e-mail, son site, etc.

Voici un exemple d’utilisation de toutes les informations :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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 . "
";
?>

J’espère que cela vous aidera. En combinant cela avec la possibilité d’ajouter des publications favorites, vous pouvez facilement créer une expérience personnalisée.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

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.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

9 commentairesLeave a Reply

  1. Stef

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

    • WPBeginner Support

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

      Admin

  2. Hash

    How to display logged in user’s profile picture ?

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

  4. ajay

    how can i echo user infomation in page

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

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

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.