After the blog fully supports HTTPS protocol access, the previous WP-ReplaceGravatarMirror plugin did not work well for HTTPS access. Therefore, a simple minor upgrade was made to the WP-ReplaceGravatarMirror plugin to support both HTTP and HTTPS websites. This improvement only optimizes for HTTPS websites. If you have enabled HTTP access, you are welcome to download and upgrade. If you have not enabled HTTP access and have already installed WP-ReplaceGravatarMirror version 1.0, you do not need to upgrade.
Plugin Name: WP-ReplaceGravatarMirror
Plugin Description: Plugin description page
Current Version: 1.1
Usage:
- Download the latest version of WP-ReplaceGravatarMirror and unzip the downloaded file. Download link: https://github.com/limccn/WP-ReplaceGravatarMirror/tree/master/release/1.1
- Upload the extracted wp-replace-gravatar-mirror folder to the plugins directory on the server, or use the built-in upload and install plugin function of Wordpress.
- Go to the Wordpress backend and activate WP-ReplaceGravatarMirror in the plugins tab.
Attached is the complete source code of WP-ReplaceGravatarMirror 1.1.
/**
* Silence is golden
*/
if (!defined('ABSPATH')) exit;
class WP_Replace_Gravatar_Mirror
{
/\*\*
\* init Hook
\*
\*/
public function \_\_construct()
{
if(isset($\_SERVER\['HTTPS'\]) && $\_SERVER\['HTTPS'\] == 'on')
{
add\_filter('get\_avatar', array($this,'replace\_gravatar\_to\_ssl'), 10, 3);
}else
{
add\_filter('get\_avatar', array($this,'replace\_gravatar\_to\_duoshuo'), 10, 3);
}
}
/\*\*
\* Use DuoShuo's gravatar mirror to replace Gravatar's.
\* Simplely replace from "\*.gravatar.com" to "gravatar.duoshuo.com".
\*
\* @param $avatar
\* @return mixed
\*/
public function replace\_gravatar\_to\_duoshuo($avatar)
{
$avatar = str\_replace(array('www.gravatar.com','0.gravatar.com','1.gravatar.com','2.gravatar.com','s.gravatar.com'),'gravatar.duoshuo.com',$avatar);
return $avatar;
}
/\*\*
\* Use https gravatar server to replace none-https.
\* Simplely replace from "http://\*.gravatar.com" to "https://secure.gravatar.com".
\*
\* @param $avatar
\* @return mixed
\*/
public function replace\_gravatar\_to\_ssl($avatar)
{
$avatar = preg\_replace('/.\*\\/avatar\\/(.\*)\\?s=(\[\\d\]+)&.\*/','![](https://secure.gravatar.com/avatar/$1?s=$2)',$avatar);
return $avatar;
}
}
/**
* bootstrap
*/
new WP_Replace_Gravatar_Mirror;