J'aime avoir des avatars montrés pour les personnes qui commentent sur mon blog. WordPress prend en charge le service gravatar nativement permettant ainsi ce qui est très facile. Cependant, il est livré avec un coût - l'accès à des fichiers externes à partir d'un autre domaine ajoute beaucoup de temps de charge supplémentaire sous la forme de nouveaux DNS lookups, nouveau SSL les connexions pour, etc. Ne serait-il pas agréable d'avoir gravatars stocké localement et servi à partir de votre propre serveur. Eh bien, c'est ce que je fais depuis un certain temps, si vous voulez savoir comment, continuer à lire…
In case you’re still wondering just why you’d want to do this, let me offer a few more benefits — images served locally can be compressed before serving them, for example ‑all my images are converted into webp versions, and any browser which supports webp gets the webp version. This is in addition to the reduction from not having to connect to an external server. The local resources can also have a caching header set on them so that browsers will cache them. Gravatar doesn’t provide for a very long cache time.
I have created 3 custom functions, que je place tout simplement dans le functions.php de mon thème. The first is a custom filter for the native wordpress get_avatar(). La deuxième avatars grappins de gravatar et Google Appelée, and the third creates a daily cronjob which refreshes the gravatars — in case they’ve been changed, or a previously unavailable one is now available.
You will need to do a little bit of tinkering to make it compatible with your theme — I use the “bones” framework as you’ll notice in the 3rd function I’ve reused some code that it provided. You will also need to create, or customise the path to store the gravatars, which in my case are stored in the theme folder in the subpath “/library/images/gravatars/”
/*********************************************\ * Filtre get_avatar à utiliser avatars locaux seulement * \*********************************************/ fonction bones_gravatar($avatar, $id_or_email, $taille, $défaut, $alt) { $root_path = get_template_directory_uri() . '/ Bibliothèque / images / gravatars /'; $root_path_local = get_template_directory() . '/ Bibliothèque / images / gravatars /'; $gravatar_path = $ root_path . 'Default_avatar'; $gravatar_path_hidpi = « data-gravatar-HiDPI ="». Root_path $ . « Default_avatar-hidpi.png"»; //shamelessly reuse original code to get the e-mail address $email = ''; si ( is_numeric($id_or_email) ) { $id = (int) $id_or_email; $user = get_userdata($la); si ( $utilisateur ) $email = $ user->user_email; } elseif ( is_object($id_or_email) ) { // No avatar for pingbacks or trackbacks $allowed_comment_types = apply_filters( '' get_avatar_comment_types, tableau( «Commentaire» ) ); si ( ! vide( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (tableau) $allowed_comment_types ) ) return false; si ( !vide($id_or_email->identifiant d'utilisateur) ) { $id = (int) $id_or_email->identifiant d'utilisateur; $user = get_userdata($la); si ( $utilisateur) $email = $ user->user_email; } elseif ( !vide($id_or_email->comment_author_email) ) { $email = $ id_or_email->comment_author_email; } } autre { $email = $ id_or_email; } si ( !vide($email) ) { $email_hash = md5( strtolower( réduire( $email ) ) ); si(le fichier existe($root_path_local . $email_hash . '.png')) { //if we have a local cache then use it $gravatar_path= $root_path . $email_hash ; $gravatar_path_hidpi = « data-gravatar-HiDPI ="». Root_path $ . $email_hash . « -hidpi.png"»; } } si($taille >= 47) $avatar = '<classe img ="charge gravatar avatar avatar - '$ size.' photo" width ="'$ Taille.'" hauteur ="'$ Taille.'" src ="» . $gravatar_path . « -hidpi.png" alt ="gravatar" />»; Sinon $ avatar = '<classe img ="charge gravatar avatar avatar - '$ size.' photo" width ="'$ Taille.'" hauteur ="'$ Taille.'" src ="» . $gravatar_path . » .png" alt ="gravatar" » . $gravatar_path_hidpi . '/>»; retour avatar $; } add_filter('Get_avatar', 'Bones_gravatar', 10, 5); /***********************\ * Créer cache gravatar * \***********************/ fonction grab_avatar($comment_id,$commentaire,$rafraîchir = false,$email = '') { tu dors(20); $root_path = get_template_directory_uri() . '/ Bibliothèque / images / gravatars /'; $root_path_local = get_template_directory() . '/ Bibliothèque / images / gravatars /'; si($rafraîchir == false) $img_name = md5( get_comment_author_email($comment_id) ); autre img_name = $ md5( $email ); si(!le fichier existe($root_path_local . $img_name . '.png') || $rafraîchir == true) { si($rafraîchir == false) $bgauthemail = get_comment_author_email(); Sinon $ bgauthemail = $ email; //try google first $domain= explode("@",$bgauthemail); si($domaine[1]=="gmail.com") { $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, faux); curl_setopt($ch, CURLOPT_RETURNTRANSFER, vrai); curl_setopt($ch, CURLOPT_URL, "https://picasaweb.google.com/data/entry/api/user/" . $bgauthemail . "?alt = json"); $résultat = curl_exec($ch); curl_close($ch); $obj = json_decode($résultat,vrai); $avatar_from_gmail = $ obj['entrée']["GPhoto $ miniature"]['$ T']; $extension = strrchr($avatar_from_gmail, ''); $ch = curl_init($avatar_from_gmail); curl_setopt( $ch, CURLOPT_NOBODY, vrai ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, faux ); curl_setopt( $ch, CURLOPT_HEADER, faux ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, vrai ); curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 ); curl_exec( $ch ); $headers = curl_getinfo( $ch ); curl_close( $ch ); si($têtes['HTTP_CODE'] === 200) { $ch = curl_init($avatar_from_gmail); $fp = fopen($root_path_local . $img_name . '-hidpi' . $extension, 'Wb'); curl_setopt($ch,CURLOPT_USERAGENT,« Mozilla / 5.0 (windows NT 6.1; Win64; x64) AppleWebKit / 537,36 (KHTML, comme Gecko) Chrome / Safari 39.0.2171.62 / 537,36'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); $usegravatar = false; $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi' . $extension); si ( ! is_wp_error( $small_image ) ) { //vérifier est pas une image de l'homme blanc, si c'est le cas, delete it if (md5_file($root_path_local . $img_name . '-hidpi.jpg') == strtolower("686E5C46776BA0E5C488853C1C0B492C")) { //delete unlink($root_path_local . $img_name . '-hidpi.jpg'); //try gravatar $usegravatar=true; } else if (md5_file($root_path_local . $img_name . '-hidpi.jpg') == strtolower("6D4083BE95FB32358A5110F5A83B9979")) { //delete unlink($root_path_local . $img_name . '-hidpi.jpg'); //try gravatar $usegravatar=true; } autre { //always convert to png because some plugins expect all gravatars to be pngs $small_image->enregistrer($root_path_local . $img_name . '-hidpi.png','Image / jpeg'); $small_image->redimensionner(40, 40, vrai); $small_image->set_quality( 10 ); $small_image->enregistrer($root_path_local . $img_name . '.png','Image / jpeg'); si($l'extension ==".jpg") séparer($root_path_local . $img_name . '-hidpi.jpg'); } } } } //check e-mail isnt a blank generic one else if($bgauthemail!=="noemail@intensedebate.com" || $usegravatar == true) { $avatar_from_gravatar = "https://www.gravatar.com/avatar/" . $img_name . "?s = 80&d = 404"; $ch = curl_init($avatar_from_gravatar); curl_setopt( $ch, CURLOPT_NOBODY, vrai ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, faux ); curl_setopt( $ch, CURLOPT_HEADER, faux ); curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, vrai ); curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 ); curl_exec( $ch ); $headers = curl_getinfo( $ch ); curl_close( $ch ); si($têtes['HTTP_CODE'] === 200) { $ch = curl_init($avatar_from_gravatar); $fp = fopen($root_path_local . $img_name . '-hidpi.png', 'Wb'); curl_setopt($ch,CURLOPT_USERAGENT,« Mozilla / 5.0 (windows NT 6.1; Win64; x64) AppleWebKit / 537,36 (KHTML, comme Gecko) Chrome / Safari 39.0.2171.62 / 537,36'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp); $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi.png'); si ( ! is_wp_error( $small_image ) ) { $small_image->redimensionner(40, 40, vrai); $small_image->enregistrer($root_path_local . $img_name . '.png'); } } } } } add_action('Wp_insert_comment', 'Grab_avatar'); /********************************************\ * Actualiser cache gravatar en arrière-plan quotidien * \********************************************/ add_action( 'Wp', 'Bones_setup_schedule' ); fonction bones_setup_schedule() { si ( ! wp_next_scheduled( 'Bones_daily_event' ) ) { wp_schedule_event( temps(), 'du quotidien', 'Bones_daily_event'); } } add_action( 'Bones_daily_event', '' bones_refresh_gravatars ); fonction bones_refresh_gravatars() { //get list of gravatars somehow global $wpdb; $comment_author_emails = $ wpdb->get_results( "SELECT DISTINCT(comment_author_email) DE " . $wpdb->préfixe . "commentaires" ); pour chaque($comment_author_emails comme $ key => $val) { pour chaque($val comme $ a => $b) { grab_avatar('','',vrai,$b); } } }
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”