Accelerated WordPress: Cache Gravatar Avatar to local

Source: Internet
Author: User
Keywords WordPress

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

What is Gravatar avatar?

Gravatar avatar is equivalent to your QQ avatar, represents your personal image in the network, so all kinds of social programs can not exempt the use of Avatar, WordPress is no exception.

As we all know, WordPress's Gravatar avatar will be the "http://1.gravatar.com/avatar/50ad321e53732e7b5a6593c3223fa9b0" format by default, and foreign servers in the celestial display speed is very slow, 8630.html "> Sometimes lead to a page because of a small head, a long time to open completely." More sometimes not even open, a big red fork, is very affecting the user experience, as well as the speed of the site open.

Today's tutorial is, the WordPress gravatar avatar cache to the local server, to speed up the operation of WordPress, if you are also a domestic server, so open speed will be much faster.

Cache Usage Prerequisites

Determine your own template or theme with WordPress 2.7 after the wp_list_comments (), if not clear, you can in your comments.php or functions.php search for a try, or do not know to skip directly.

Cache Gravatar Avatar Build cache Directory

To create a folder at Wp-content's sibling directory: Avatar, folder permissions are: 755, if not, the folder permissions are set to 777, which is the path of the Gravatar avatar cache.

Set default Avatar

Make a default avatar, you can also directly in the WordPress background "set" a copy of the discussion, named "Default.jpg", placed in the Avatar folder.

Add cache Code

Copy the following code into the template's functions.php file:

function My_avatar ($avatar) {$tmp = Strpos ($avatar, ' http '); $g = substr ($avatar, $tmp, Strpos ($avatar, "", $tmp)-$tmp ); $tmp = Strpos ($g, ' avatar/') + 7; $f = substr ($g, $tmp, Strpos ($g, "?", $tmp)-$tmp); $w = Get_bloginfo (' Wpurl '); $e = Abspath. ' Avatar/'. $f. JPG '; $t = 1209600; Set 14 days, Unit: SEC if (!is_file ($e) | | (Time ()-Filemtime ($e)) > $t) {//When the image does not exist or files exceed 14 days to update copy (Htmlspecialchars_decode ($g), $e); else $avatar = STRTR ($av ATAR, Array ($g => $w. '/avatar/'. $f. jpg ')); if (FileSize ($e) <) copy ($w. '/avatar/default.jpg ', $e); return $avatar; } add_filter (' Get_avatar ', ' My_avatar ');

OK, wrap it up. Refresh a few pages to see if the Avatar folder has cached content, if not, the cache failed, the method is not for you. Specific code from Willin, how to set the Avatar please Baidu, a lot of similar tutorials, respect for the work of others, reproduced please specify from the WordPress fan: http://www.wpmee.com, thank you for your cooperation!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.