X

Comment afficher l’avatar de l’utilisateur actuellement connecté avec get_avatar ?

Snippets by IsItWP

Vous cherchez un moyen d’afficher l’avatar d’un utilisateur connecté dans votre thème ? Bien qu’il existe probablement un plugin pour cela, nous avons créé un extrait de code rapide que vous pouvez utiliser pour afficher l’avatar de l’utilisateur connecté avec get_avatar dans WordPress.

Pour ajuster la taille de l’avatar, vous pouvez changer le nombre 64 dans la fonction get_avatar pour la valeur par défaut qui est de 96 et la taille maximale est de 512.

Instructions:

Tout ce que vous avez à faire est d’ajouter ce code au fichier index.php de votre thème :

 <?php
        global $current_user;
        get_currentuserinfo();     
        echo get_avatar( $current_user->ID, 64 );
 ?>

Note : Si c’est la première fois que vous ajoutez des extraits de code dans WordPress, veuillez vous référer à notre guide sur la façon d’ajouter correctement des extraits de code dans WordPress, afin de ne pas briser accidentellement votre site.

Si vous avez aimé cet extrait de code, n’hésitez pas à consulter nos autres articles sur le site comme : 28 meilleurs thèmes WordPress pour votre site web et comment créer un formulaire d’enregistrement d’utilisateur personnalisé sur WordPress.

Commentaires   laisser une réponse

  1. Hello Kevin,
    thanks for your great php-work and support.

    I searched nearly everywhere on the internet (but darknet…), to find a solution for this.

    I want to implement your code snippet into my wordpress – buddypress.
    All users should be able to login via a button, which shows the avatar after login.

    The Problem is, I want to show it up in the right of my topbar, but I don’t know, what the html should look like.
    I am familiar with css and very little to html.

    Thank you so far.

  2. I want a short code to display the avatar for the current login user.

    What do I need to add to [show_avatar]

  3. How can one Output the code in a shortcode please… I want display it in a colomn on the frontend

  4. thank you so much, great solution!

  5. Kevin ur GREAT lovely Thanks very much

    1. Kevin one more thing is that even after logged-out default image is seen instead of no image is there any solution by the way iam using in navigation
      can that be made to work also as drop down menu

  6. Kevin ur GREAT lovely Thanks very much

  7. sorry in advance for my lack of knowledge with wordpress. i placed the piece of code in my child theme (main index template – index.php) file. how would i call up or add the avatar to a sidebar widget? sorry again and thank you for your time.

  8. Sara Ben Chamkha octobre 25, 2016 à 6:48 am

    any wordpress plugin to show name + avatar of logged in users in topbar ?

  9. Thanks for this!

    Is there a way to have the users name also show up to the left as well?

    Would also be so cool if you did a post on how to show all three main elements for logged in users avatar, username x profile field, cover image and maybe friend/ follower number!

    Believe me i have searched high and low for posts on this and cannot find anywhere eon entire internet.

    thanks!

    1. Hi James,
      WordPress does not have “friend/ follower number” are you using BuddyPress ?

      global $current_user;
      get_currentuserinfo();
      echo get_avatar( $current_user->ID, 64 );
      echo ‘Username: ‘ . $current_user->user_login . “n”;
      echo ‘User email: ‘ . $current_user->user_email . “n”;
      echo ‘User level: ‘ . $current_user->user_level . “n”;
      echo ‘User first name: ‘ . $current_user->user_firstname . “n”;
      echo ‘User last name: ‘ . $current_user->user_lastname . “n”;
      echo ‘User display name: ‘ . $current_user->display_name . “n”;
      echo ‘User ID: ‘ . $current_user->ID . “n”;

      The following will get all the values from the current user then you just have to write some HTML, CSS to display things the way you would like e.g.:

      user_firstname . “n”; ?>

      and so on within your template.

      1. Thank you this is so useful!!!!!! 🙂

        1. no problem anytime.

  10. Hello experts,
    I was just wondering if anyone knows how to place a login user name and disply user avatar on the top menu bar as like wordpress admin bar?

    I’m using jarida theme

    Thanks

  11. This might be a stupid question but where do I add this snippet?

    1. You should be able to place this almost anyplace in a template.

  12. what if i want to show it for non login user?

  13. nice, thank u so much….

  14. you saved another life thank you!

  15. THANK YOU SOOOOO MUCH!! YOU SAVED ME !!!! 😀 IT REALLY HELPED!!

  16. i want ur code…pls

  17. did you get my last message!?

    1. I’m going to post a snippet tomorrow with the author avatar just for you. This should solve your problems.

      1. brillaint! – ur a star mate!

  18. thanks so much ive been looking for a bit of code to use with the author box plugin …… thanks again!

    1. No problem enjoy the snippet!

      1. u know what … i just realised that if i log out that the avatar disappears!! 🙁 how do i keep it inplace constantly – im currently using the snippet to display a facebook profile avatar with a about author plugin!? cheers

        1. This snippet shows the avatar of the logged in user so it will not display anything when your not logged in. If you can let me know what you want to do I may be able to help.

  19. Hello!

    I have a problem, when reply a comment the avatar disappers…do you know how to solve this?

    1. this snippet just shows the current avatar of the logged in user only. If you want to display other users avatars you need to use another method.

      1. the avatar disappers when I try to reply a comment … the snippet not working when try to reply.

        1. Do you have this online so I can take a look ? use the contact form to email the link. http://wpsnipp.com/contact/

        2. problem solved , when hit reply the avatar take the default css style

          .commentlist .avatar {
              left: 0;    position: absolute;    top: 11px;
          }

          this did the trik

          #respond .comment-icon .avatar {    left: 0;    top: 0;}thanks anyways.

  20. Thanks for the php coding….

    1. No problem glad that I could help out!

Ajouter un commentaire

Nous sommes heureux que vous ayez choisi de laisser un commentaire. N'oubliez pas que tous les commentaires sont modérés conformément à notre privacy policy, et que tous les liens sont en nofollow. N'utilisez PAS de mots-clés dans le champ du nom. Engageons une conversation personnelle et constructive.

WordPress Launch Checklist

L'ultime liste de contrôle pour le lancement de WordPress

Nous avons rassemblé tous les éléments essentiels de la liste de contrôle pour le lancement de votre prochain site Web WordPress dans un ebook pratique.
Oui, envoyez-moi le gratuit !