国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
詳解WordPress中的頭像緩存和代理中的緩存更新方法,wordpress頭像
您可能感興趣的文章:
Home Backend Development PHP Tutorial Detailed explanation of avatar cache in WordPress and cache update method in proxy, wordpress avatar_PHP tutorial

Detailed explanation of avatar cache in WordPress and cache update method in proxy, wordpress avatar_PHP tutorial

Jul 12, 2016 am 08:58 AM
wordpress cache

詳解WordPress中的頭像緩存和代理中的緩存更新方法,wordpress頭像

wordpress評論中的頭像是使用Gravatar的頭像服務(wù)(Gravatar官方注冊地址:http://en.gravatar.com),用戶的緩存頭像一般都是固定不變的,所以我們可以將頭像緩存到本地來提高我們網(wǎng)站的訪問速度。
我的wordpress avatar目錄的頭像緩存:

201631141915179.jpg (280×244)

wordpress頭像緩存功能設(shè)置方法
首先是在根目錄下建立一個文件夾avatar,權(quán)限755。再在里面放一個默認(rèn)的頭像(default.jpg),沒頭像的童鞋就會用默認(rèn)的。代碼如下:

function my_avatar( $email, $size = '32', $default = '', $alt = '') {
 $f = md5( strtolower( $email ) );
 $a = WP_CONTENT_URL . '/avatar/'. $f . $size . '.png';
 $e = WP_CONTENT_DIR . '/avatar/' . $f . $size . '.png';
 $d = WP_CONTENT_DIR . '/avatar/' . $f . '-d.png';

 if($default=='')
  $default = 'http://www.wpnoob.cn/avatar/default.jpg'; //尺寸需要改為你自己網(wǎng)站評論的默認(rèn)頭像
 
 $t = 2592000; // 緩存有效期30天, 這里單位:秒
 if ( !is_file($e) || (time() - filemtime($e)) > $t ) {
  if ( !is_file($d) || (time() - filemtime($d)) > $t ) {
   // 驗(yàn)證是否有頭像
   $uri = 'http://www.gravatar.com/avatar/' . $f . '?d=404';
   $headers = @get_headers($uri);
   if (!preg_match("|200|", $headers[0])) {
    // 沒有頭像,則新建一個空白文件作為標(biāo)記
    $handle = fopen($d, 'w');
    fclose($handle);

    $a = $default;
   }
   else {
    // 有頭像且不存在則更新
    $r = get_option('avatar_rating');
    $g = 'http://www.gravatar.com/avatar/'. $f. '?s='. $size. '&r=' . $r;
    copy($g, $e);
   }
  }
  else {
   $a = $default;
  }
 }
 
 $avatar = "<img alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
 return apply_filters('my_avatar', $avatar, $email, $size, $default, $alt);
}

再將以上代碼添加到你主題的functions.php文件。
將獲取頭像地址的 get_avatar 函數(shù)替換為 my_avatar 。有個例外,functions.php評論列表函數(shù)中:

get_avatar( $comment

需要改成:

my_avatar( $comment->comment_author_email

因?yàn)閙y_avatar函數(shù)只能通過Email來調(diào)取用戶頭像,所以以上情況,需要將第一個參數(shù)改成email地址。

get_avatar函數(shù)介紹:
用上面的方法簡單方便啊。 不過還有一步是要注意的。得要確認(rèn)在調(diào)用頭像的地方都是用get_avatar函數(shù)來完成的。一般都是了,只有以前老的theme才不是。不是的話改過來就行。

如改為:

<&#63;php
 echo get_avatar( $comment->comment_author_email, $size = '48', $default = get_bloginfo('wpurl') . '/avatar/default.jpg' ); 
&#63;>

代理(squid)中更新css/js文件緩存的方法
在wordpress添加css或者js文件,我們一般使用這四個函數(shù)來實(shí)現(xiàn):

  • wp_enqueue_script()
  • wp_enqueue_style()
  • wp_register_script()
  • wp_register_style()

函數(shù)中你可以定義css/js的版本號,以便我們在對css/js文件更新時能夠清楚瀏覽器的緩存,默認(rèn)的版本號是wordpress的版本號。版本號會鏈接在css/js完整路徑的后面,一般在版本號變更后,css/js載入的樣式的完整URL也會變更,瀏覽器發(fā)現(xiàn)URL變更會重新請求css/js文件,這樣就能達(dá)到載入最新的css/js文件。

但是很多代理軟件(比如squid)并不支持”?“號形式的cache,我們在使用反向代理來cache我們的網(wǎng)站時,特別在squid3.0以后,已經(jīng)開始不對帶”?”號的url進(jìn)行緩存了。所以我們?nèi)绻褂胹quid的緩存功能就必須去掉”?”,更新squid代理商的緩存只能通過修改文件名來實(shí)現(xiàn)。

以下我們將介紹在wordpress通過對版本號的控制來修改js/css文件名從而能夠在代理軟件中達(dá)到緩存的目的:
1、在我們的主題代碼functions.php文件中添加如下代碼:

/** 
 * Description: wordpress在代理(squid)中更新css/js文件緩存的方法
 * Author:wordpress教程網(wǎng)
 * Author URI: http://www.wpnoob.cn/
 */
function ds_filename_based_cache_busting( $src ) {
 // 管理員的后臺css/js文件無需處理
 if ( is_admin() )
 return $src;
 //將版本號添加到文件名中已”.“號來區(qū)分
 return preg_replace(
 '/\.(js|css)\&#63;ver=(.+)$/',
 '.$2.$1',
 $src
 );
}
add_filter( 'script_loader_src', 'ds_filename_based_cache_busting' );
add_filter( 'style_loader_src', 'ds_filename_based_cache_busting' );

如果你使用的是apache服務(wù)器,在你的根目錄的.htaccess文件下添加:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
 
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.+)\.(.+)\.(js|css)$ $1.$3 [L]
</IfModule>


如果你是nginx服務(wù)器配置如下:

location ~ ^(.+)\.(.+)\.(js|css)$ {
  alias $1.$3;
}&#65279;

您可能感興趣的文章:

  • WordPress中Gravatar頭像緩存到本地及相關(guān)優(yōu)化的技巧

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1105380.htmlTechArticle詳解WordPress中的頭像緩存和代理中的緩存更新方法,wordpress頭像 wordpress評論中的頭像是使用Gravatar的頭像服務(wù)(Gravatar官方注冊地址:http...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to adjust the wordpress article list How to adjust the wordpress article list Apr 20, 2025 am 10:48 AM

There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

10 latest tools for web developers 10 latest tools for web developers May 07, 2025 pm 04:48 PM

Web development design is a promising career field. However, this industry also faces many challenges. As more businesses and brands turn to the online marketplace, web developers have the opportunity to demonstrate their skills and succeed in their careers. However, as demand for web development continues to grow, the number of developers is also increasing, resulting in increasingly fierce competition. But it’s exciting that if you have the talent and will, you can always find new ways to create unique designs and ideas. As a web developer, you may need to keep looking for new tools and resources. These new tools and resources not only make your job more convenient, but also improve the quality of your work, thus helping you win more business and customers. The trends of web development are constantly changing.

How to import the source code of wordpress How to import the source code of wordpress Apr 20, 2025 am 11:24 AM

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

How to build a website for wordpress host How to build a website for wordpress host Apr 20, 2025 am 11:12 AM

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

How to add your WordPress site in Yandex Webmaster Tools How to add your WordPress site in Yandex Webmaster Tools May 12, 2025 pm 09:06 PM

Do you want to connect your website to Yandex Webmaster Tools? Webmaster tools such as Google Search Console, Bing and Yandex can help you optimize your website, monitor traffic, manage robots.txt, check for website errors, and more. In this article, we will share how to add your WordPress website to the Yandex Webmaster Tool to monitor your search engine traffic. What is Yandex? Yandex is a popular search engine based in Russia, similar to Google and Bing. You can excel in Yandex

How to set, get and delete WordPress cookies (like a professional) How to set, get and delete WordPress cookies (like a professional) May 12, 2025 pm 08:57 PM

Do you want to know how to use cookies on your WordPress website? Cookies are useful tools for storing temporary information in users’ browsers. You can use this information to enhance the user experience through personalization and behavioral targeting. In this ultimate guide, we will show you how to set, get, and delete WordPresscookies like a professional. Note: This is an advanced tutorial. It requires you to be proficient in HTML, CSS, WordPress websites and PHP. What are cookies? Cookies are created and stored when users visit websites.

How to fix HTTP image upload errors in WordPress (simple) How to fix HTTP image upload errors in WordPress (simple) May 12, 2025 pm 09:03 PM

Do you need to fix HTTP image upload errors in WordPress? This error can be particularly frustrating when you create content in WordPress. This usually happens when you upload images or other files to your CMS using the built-in WordPress media library. In this article, we will show you how to easily fix HTTP image upload errors in WordPress. What is the reason for HTTP errors during WordPress media uploading? When you try to upload files to Wo using WordPress media uploader

How to register a wordpress account How to register a wordpress account Apr 20, 2025 am 11:45 AM

To create an account on WordPress, simply visit its website, select the registration option, fill in the registration form, and verify your email address. Other ways to register include using a Google account or Apple ID. The benefits of signing up include creating a website, gaining features, joining the community, and gaining support.

See all articles