WordPress can't show Gravatar avatar solution

Source: Internet
Author: User
Tags php file

According to the feedback from the network users, through the use of well-known social comment service providers to say that the provision of domestic Gravatar services, replace the default Gravatar function can solve the problem, and the access speed is very good.

Workaround 1: Use Gravatar HTTPS (apply to any topic)

In the topic directory, locate and open the functions.php file, and then copy the following code into the file:

The code is as follows Copy Code

Hackgravatatr
function Wpyou_get_ssl_avatar ($avatar) {
$avatar = Preg_replace ('/.*\/avatar\/(. *) \?s= ([\d]+) &.*/', ' return $avatar;
}
Add_filter (' Get_avatar ', ' Wpyou_get_ssl_avatar ');

Workaround 2: Use the multi-say Avatar library for replacement (for any subject)

In the topic directory, locate and open the functions.php file, and then copy the following code into the file:

The code is as follows Copy Code

Hackgravatatr
function Wpyou_get_avatar ($avatar) {
$avatar = Str_replace (Array ("www.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), " Gravatar.duoshuo.com ", $avatar);
return $avatar;
}
Add_filter (' Get_avatar ', ' Wpyou_get_avatar ', 10, 3);

The code is correctly added to the theme of the functions.php file can be used to provide more than the domestic Gravatar service, so that the perfect solution to the problem.

Workaround 3: Local cache Avatar

3.1, set up a cache directory

Set up a folder in the Wp-content directory, named Avatar, and set the permissions for the folder to 0755 (if 0755 is not possible, try 0777).

3.2, set the default avatar

Prepare a default avatar with a size fit, named "Default.jpg", and put it in the Avatar folder.

3.3. Add Cache Code

In the topic directory, locate and open the functions.php file, and then copy the following code into the file:

The code is as follows Copy Code

//Hackgravatatr
Function Wpyou_localcache_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: Seconds
if (!is_file ($e) | | (Time ()-Filemtime ($e)) > $t) {//When avatar does not exist or files exceed 14 days to update
copy (Htmlspecialchars_decode ($g), $e);
Else $avatar = s TRTR ($avatar, Array ($g => $w. '/avatar/'. $f. jpg '));
if (filesize ($e) <) copy ($w. '/avatar/default.jpg ', $e);
return $avatar;
}
Add_filter (' Get_avatar ', ' Wpyou_localcache_avatar ');

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.