أحب أن يكون الآلهة يظهر للأشخاص الذين التعليق على بلدي بلوق. وورد يدعم خدمة غرفتر أصلا حتى تمكن هذا أمر سهل جدا. ولكن لأنه يأتي مع التكلفة - الوصول إلى الملفات الخارجية من مجال آخر يضيف الكثير من وقت التحميل الإضافي في شكل جديد DNS عمليات البحث, جديد SSL اتصالات لجعل, إلخ. Wouldn’t it be nice to have gravatars stored locally and served from your own server. حسنا هذا ما قمت به لبعض الوقت الآن, إذا كنت ترغب في معرفة كيف, واصل القراءة…
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, وأنا ببساطة مكان في functions.php موضوعي ل. The first is a custom filter for the native wordpress get_avatar(). العناوين الرئيسية للصحف الآلهة الثانية من غرفتر وجوجل عندما يطلب منها ذلك, 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 3بحث وتطوير 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/”
/*********************************************\ * تصفية get_avatar استخدام الآلهة المحلية فقط * \*********************************************/ وظيفة bones_gravatar($الصورة الرمزية لل, $id_or_email, $بحجم, $الافتراضي, $بديل) { $root_path = get_template_directory_uri() . '/library/images/gravatars/'; $root_path_local = get_template_directory() . '/library/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 = ''; إذا ( is_numeric($id_or_email) ) { $معرف = (الباحث) $id_or_email; $المستخدم = get_userdata($هوية شخصية); إذا ( $المستعمل ) $البريد الإلكتروني = $ من قبل المستخدم>USER_EMAIL; } ELSEIF ( is_object($id_or_email) ) { // No avatar for pingbacks or trackbacks $allowed_comment_types = apply_filters( 'get_avatar_comment_types', مجموعة( 'comment' ) ); إذا ( ! فارغة( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (مجموعة) $allowed_comment_types ) ) عودة كاذبة; إذا ( !فارغة($id_or_email->معرف المستخدم) ) { $معرف = (الباحث) $id_or_email->معرف المستخدم; $المستخدم = get_userdata($هوية شخصية); إذا ( $المستعمل) $البريد الإلكتروني = $ من قبل المستخدم>USER_EMAIL; } ELSEIF ( !فارغة($id_or_email->comment_author_email) ) { $البريد الإلكتروني = $ id_or_email->comment_author_email; } } آخر { $البريد الإلكتروني = $ id_or_email; } إذا ( !فارغة($البريد الإلكتروني) ) { $email_hash = MD5( strtolower( تقليم( $البريد الإلكتروني ) ) ); إذا(الملف موجود($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"'; } } إذا($بحجم >= 47) $avatar= '<IMG الطبقة ="load-gravatar avatar avatar-'.$size.' photo" العرض ="'.$size.'" ارتفاع ="'.$size.'" SRC ="' . $gravatar_path . '-hidpi.png" بديل ="غرفتر" />'; else $avatar= '<IMG الطبقة ="load-gravatar avatar avatar-'.$size.' photo" العرض ="'.$size.'" ارتفاع ="'.$size.'" SRC ="' . $gravatar_path . '.png" بديل ="غرفتر" ' . $gravatar_path_hidpi . ' />'; عودة $ الرمزية; } add_filter('get_avatar', 'bones_gravatar', 10, 5); /***********************\ * إنشاء مخبأ غرفتر * \***********************/ وظيفة grab_avatar($comment_id,$التعليق,$تحديث = كاذبة,$email='') { انت نائم(20); $root_path = get_template_directory_uri() . '/library/images/gravatars/'; $root_path_local = get_template_directory() . '/library/images/gravatars/'; إذا($تحديث == كاذبة) $img_name = MD5( get_comment_author_email($comment_id) ); آخر $ img_name = MD5( $البريد الإلكتروني ); إذا(!الملف موجود($root_path_local . $img_name . '.png') || $تحديث == صحيح) { إذا($تحديث == كاذبة) $bgauthemail = get_comment_author_email(); آخر $ bgauthemail = $ البريد الإلكتروني; //try google first $domain= explode("@",$bgauthemail); إذا($مجال[1]=="gmail.com") { $الفصل = curl_init(); curl_setopt($الفصل, CURLOPT_SSL_VERIFYPEER, خاطئة); curl_setopt($الفصل, CURLOPT_RETURNTRANSFER, حقيقية); curl_setopt($الفصل, CURLOPT_URL, "https://picasaweb.google.com/data/entry/api/user/" . $bgauthemail . "?بديل = سلمان"); $النتيجة = curl_exec($الفصل); curl_close($الفصل); $الكائنات = json_decode($نتيجة,حقيقية); $avatar_from_gmail = $ الكائنات['entry']['gphoto$thumbnail']['$t']; $تمديد = strrchr($avatar_from_gmail, '.'); $الفصل = curl_init($avatar_from_gmail); curl_setopt( $الفصل, CURLOPT_NOBODY, حقيقية ); curl_setopt( $الفصل, CURLOPT_RETURNTRANSFER, خاطئة ); curl_setopt( $الفصل, CURLOPT_HEADER, خاطئة ); curl_setopt( $الفصل, CURLOPT_FOLLOWLOCATION, حقيقية ); curl_setopt( $الفصل, CURLOPT_MAXREDIRS, 3 ); curl_exec( $الفصل ); $رؤوس = curl_getinfo( $الفصل ); curl_close( $الفصل ); إذا($رؤوس['http_code'] === 200) { $الفصل = curl_init($avatar_from_gmail); $FP = الدالة fopen($root_path_local . $img_name . '-hidpi' . $تمديد, 'wb'); curl_setopt($الفصل,CURLOPT_USERAGENT,'Mozilla/5.0 (ويندوز NT 6.1; Win64; إلى x64) AppleWebKit / 537.36 (KHTML, مثل أبو بريص) Chrome/39.0.2171.62 Safari/537.36'); curl_setopt($الفصل, CURLOPT_FILE, $FP); curl_setopt($الفصل, CURLOPT_HEADER, 0); curl_exec($الفصل); curl_close($الفصل); fclose($FP); $usegravatar = كاذبة; $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi' . $تمديد); إذا ( ! is_wp_error( $small_image ) ) { //check it isn't a blank man image, لو ذلك, 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; } آخر إذا (md5_file($root_path_local . $img_name . '-hidpi.jpg') == strtolower("6D4083BE95FB32358A5110F5A83B9979")) { //delete unlink($root_path_local . $img_name . '-hidpi.jpg'); //try gravatar $usegravatar=true; } آخر { //always convert to png because some plugins expect all gravatars to be pngs $small_image->حفظ($root_path_local . $img_name . '-hidpi.png','image/png'); $small_image->تغيير(40, 40, حقيقية); $small_image->set_quality( 10 ); $small_image->حفظ($root_path_local . $img_name . '.png','image/png'); إذا($تمديد ==".JPG") فك ارتباط($root_path_local . $img_name . '-hidpi.jpg'); } } } } //check e-mail isnt a blank generic one else if($bgauthemail!=="noemail@intensedebate.com" || $usegravatar == صحيح) { $avatar_from_gravatar = "https://www.gravatar.com/avatar/" . $img_name . "?الصورة = 80&د = 404"; $الفصل = curl_init($avatar_from_gravatar); curl_setopt( $الفصل, CURLOPT_NOBODY, حقيقية ); curl_setopt( $الفصل, CURLOPT_RETURNTRANSFER, خاطئة ); curl_setopt( $الفصل, CURLOPT_HEADER, خاطئة ); curl_setopt( $الفصل, CURLOPT_FOLLOWLOCATION, حقيقية ); curl_setopt( $الفصل, CURLOPT_MAXREDIRS, 3 ); curl_exec( $الفصل ); $رؤوس = curl_getinfo( $الفصل ); curl_close( $الفصل ); إذا($رؤوس['http_code'] === 200) { $الفصل = curl_init($avatar_from_gravatar); $FP = الدالة fopen($root_path_local . $img_name . '-hidpi.png', 'wb'); curl_setopt($الفصل,CURLOPT_USERAGENT,'Mozilla/5.0 (ويندوز NT 6.1; Win64; إلى x64) AppleWebKit / 537.36 (KHTML, مثل أبو بريص) Chrome/39.0.2171.62 Safari/537.36'); curl_setopt($الفصل, CURLOPT_FILE, $FP); curl_setopt($الفصل, CURLOPT_HEADER, 0); curl_exec($الفصل); curl_close($الفصل); fclose($FP); $small_image = wp_get_image_editor($root_path_local . $img_name . '-hidpi.png'); إذا ( ! is_wp_error( $small_image ) ) { $small_image->تغيير(40, 40, حقيقية); $small_image->حفظ($root_path_local . $img_name . '.png'); } } } } } ADD_ACTION('wp_insert_comment', 'grab_avatar'); /********************************************\ * تحديث مخبأ غرفتر في الخلفية يوميا * \********************************************/ ADD_ACTION( 'wp', 'bones_setup_schedule' ); وظيفة bones_setup_schedule() { إذا ( ! wp_next_scheduled( 'bones_daily_event' ) ) { wp_schedule_event( زمن(), 'daily', 'bones_daily_event'); } } ADD_ACTION( 'bones_daily_event', 'bones_refresh_gravatars' ); bones_refresh_gravatars وظيفة() { //get list of gravatars somehow global $wpdb; $comment_author_emails = $ wpdb->الحصول على النتائج( "SELECT DISTINCT(comment_author_email) من عند " . $wpdb->اختصار . "تعليقات" ); foreach($comment_author_emails كمفتاح $ => $فال) { foreach($فال من $ A => $ب) { grab_avatar('','',حقيقية,$ب); } } }
“Hi James I realise it has been a long while, but I just checked this on windows 11 (build 23H2)…”