Eu gosto de ter avatares exibidos para pessoas que comentam no meu blog. WordPress suporta o serviço gravatar nativamente permitindo assim isso é muito fácil. No entanto ele vem com um custo - acesso a arquivos externos de outro domínio acrescenta lotes de tempo de carregamento extra na forma de novos DNS pesquisas, novo SSL conexões para fazer, etc. Wouldn’t it be nice to have gravatars stored locally and served from your own server. Bem, isso é o que eu venho fazendo há algum tempo, se você gostaria de saber como, Leia…
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 eu simplesmente colocar em functions.php do meu tema. The first is a custom filter for the native wordpress get_avatar(). O segundo agarra avatares de gravatar e google quando chamados, 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/”
/*********************************************\ * Filtrar get_avatar usar avatares locais apenas * \*********************************************/ bones_gravatar função($avatar, $id_or_email, $tamanho, $omissão, $alt) { $root_path = get_template_directory_uri() . '/ Biblioteca / images / gravatars /'; $root_path_local = get_template_directory() . '/ Biblioteca / images / gravatars /'; $gravatar_path = $ root_path . 'Default_avatar'; $gravatar_path_hidpi = 'dados-gravatar-HiDPI ="'. $ Root_path . 'Default_avatar-hidpi.png"'; //shamelessly reuse original code to get the e-mail address $email = ''; se ( is_numeric($id_or_email) ) { $id = (int) $id_or_email; $usuário = get_userdata($o); se ( $do utilizador ) $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', ordem( 'Comentário' ) ); se ( ! vazio( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (ordem) $allowed_comment_types ) ) return false; se ( !vazio($id_or_email->ID do usuário) ) { $id = (int) $id_or_email->ID do usuário; $usuário = get_userdata($o); se ( $do utilizador) $email = $ user->user_email; } elseif ( !vazio($id_or_email->comment_author_email) ) { $email = $ id_or_email->comment_author_email; } } outro { $email = $ id_or_email; } se ( !vazio($o email) ) { $email_hash = MD5( strtolower( aparar( $o email ) ) ); se(o arquivo 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 = 'dados-gravatar-HiDPI ="'. $ Root_path . $email_hash . '-hidpi.png"'; } } se($tamanho >= 47) $avatar = '<img classe ="avatar avatar gravatar carga - '$ tamanho.' foto" largura ="'$ Tamanho.'" altura ="'$ Tamanho.'" src ="' . $gravatar_path . '-hidpi.png" alt ="gravatar" />'; outra $ avatar = '<img classe ="avatar avatar gravatar carga - '$ tamanho.' foto" largura ="'$ Tamanho.'" altura ="'$ Tamanho.'" src ="' . $gravatar_path . ' .png" alt ="gravatar" ' . $gravatar_path_hidpi . '/>'; return $ avatar; } add_filter('Get_avatar', 'Bones_gravatar', 10, 5); /***********************\ * Criar cache de gravatar * \***********************/ grab_avatar função($comment_id,$comentário,$refrescar = false,$email = '') { você dorme(20); $root_path = get_template_directory_uri() . '/ Biblioteca / images / gravatars /'; $root_path_local = get_template_directory() . '/ Biblioteca / images / gravatars /'; se($atualizar == false) $img_name = MD5( get_comment_author_email($comment_id) ); outra $ img_name = md5( $o email ); se(!o arquivo existe($root_path_local . $img_name . '.png') || $atualizar == true) { se($atualizar == false) $bgauthemail = get_comment_author_email(); outra $ bgauthemail = $ email; //try google first $domain= explode("@",$bgauthemail); se($domínio[1]=="gmail.com") { $CH = curl_init(); curl_setopt($CH, CURLOPT_SSL_VERIFYPEER, falso); curl_setopt($CH, CURLOPT_RETURNTRANSFER, true); curl_setopt($CH, CURLOPT_URL, "https://picasaweb.google.com/data/entry/api/user/" . $bgauthemail . "?alt = json"); $resultado = curl_exec($CH); curl_close($CH); $obj = json_decode($resultado,true); $avatar_from_gmail = $ obj['entrada']["GPhoto $ miniaturas"]['$ T']; $extensão = strrchr($avatar_from_gmail, ''); $CH = curl_init($avatar_from_gmail); curl_setopt( $CH, CURLOPT_NOBODY, true ); curl_setopt( $CH, CURLOPT_RETURNTRANSFER, falso ); curl_setopt( $CH, CURLOPT_HEADER, falso ); curl_setopt( $CH, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $CH, CURLOPT_MAXREDIRS, 3 ); curl_exec( $CH ); $cabeçalhos = curl_getinfo( $CH ); curl_close( $CH ); se($cabeçalhos['HTTP_CODE'] === 200) { $CH = curl_init($avatar_from_gmail); $fp = fopen($root_path_local . $img_name . '-hidpi' . $extensão, 'Wb'); curl_setopt($CH,CURLOPT_USERAGENT,'Mozilla / 5.0 (Windows NT 6.1; Win64; x64) AppleWebKit / 537,36 (KHTML, como Gecko) Chrome / 39.0.2171.62 Safari / 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' . $extensão); se ( ! is_wp_error( $small_image ) ) { //verificá-lo não é uma imagem de homem branco, se então, 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; } outro { //always convert to png because some plugins expect all gravatars to be pngs $small_image->Salve ($root_path_local . $img_name . '-hidpi.png','Image / png'); $small_image->redimensionar(40, 40, true); $small_image->set_quality( 10 ); $small_image->Salve ($root_path_local . $img_name . '.png','Image / png'); se($extensão ==".jpg") desligar($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, true ); curl_setopt( $CH, CURLOPT_RETURNTRANSFER, falso ); curl_setopt( $CH, CURLOPT_HEADER, falso ); curl_setopt( $CH, CURLOPT_FOLLOWLOCATION, true ); curl_setopt( $CH, CURLOPT_MAXREDIRS, 3 ); curl_exec( $CH ); $cabeçalhos = curl_getinfo( $CH ); curl_close( $CH ); se($cabeçalhos['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, como Gecko) Chrome / 39.0.2171.62 Safari / 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'); se ( ! is_wp_error( $small_image ) ) { $small_image->redimensionar(40, 40, true); $small_image->Salve ($root_path_local . $img_name . '.png'); } } } } } add_action('Wp_insert_comment', 'Grab_avatar'); /********************************************\ * Atualizar o cache gravatar no fundo diária * \********************************************/ add_action( 'Wp', 'Bones_setup_schedule' ); bones_setup_schedule função() { se ( ! wp_next_scheduled( 'Bones_daily_event' ) ) { wp_schedule_event( Tempo(), 'diariamente', 'Bones_daily_event'); } } add_action( 'Bones_daily_event', 'bones_refresh_gravatars' ); bones_refresh_gravatars função() { //get list of gravatars somehow global $wpdb; $comment_author_emails = $ wpdb->get_results( "SELECT DISTINCT(comment_author_email) A PARTIR DE " . $wpdb->prefixo . "comentários" ); para cada($comment_author_emails como $ key => $val) { para cada($val quanto $ a => $b) { grab_avatar('','',true,$b); } } }
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”