不用插件实现WordPress随机自动变换头像的方法

2013年10月24日

/* 原创内容,转载请注明出处:https://www.myzhenai.com/thread-15963-1-1.html */
关键字:WordPress 头像 自定义 随机 自动更换
我是php初学者,闲来无事拿WordPress练手的,所以高手请不要见笑了.网上有很多自定义WordPress头像的方法,但缺点是只能有一张图片,如果想要多张图片做为头像并且自动变换的方法一直没有.我是看着php中文文档自己折腾一晚上才设置成功的,不需要插件,只不过是要修改几个文件里的一些代码来实现.我的想法是用数组来对应目录下的相关图标文件实现,方法其实很简单.步骤如下
1:准备一组图片,准备做为头像使用的,例如我的就准备了15张图片,分别将图片名称修改为avatar0.png avatar1.png avatar2.png….的顺序.在服务器根目录新建一个/images/avatar/两级目录,将所有图片上传到/images/avatar/下.
2:操作之前请备份好你的相关文件,即需要修改的两个文件./wp-includes/pluggable.php /wp-admin/options-discussion.php
3:用UltraEdit或gedit等文本编辑器打开这两个文件. 查找 get_avatar

/wp-admin/options-discussion.php

$avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar);

 
替换

$avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&forcedefault=1'", $avatar);

 

/wp-includes/pluggable.php

if ( !function_exists( 'get_avatar' ) ) :
/**
 * Retrieve the avatar for a user who provided a user ID or email address.
 *
 * @since 2.5
 * @param int|string|object $id_or_email A user ID,  email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternative text to use in image tag. Defaults to blank
 * @return string  tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
$Picture = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); /* 新建一个数组,有15个键名 */
$Random = array_rand($Picture); /* 随机选择数组里的一个键名 */
$host = "https://jiayu.mybabya.com/images/avatar/avatar"; /* 设置一个文本变量,内容是网址. */
$Suffix = "png"; /* 设置一个文本变量,内容是后缀名,php中变量相加采用. 例如 $host.$Random.$Suffix */
	if ( ! get_option('show_avatars') )
		return false;

	if ( false === $alt)
		$safe_alt = '';
	else
		$safe_alt = esc_attr( $alt );

	if ( !is_numeric($size) )
		$size = '96';

	$email = '';
	if ( is_numeric($id_or_email) ) {
		$id = (int) $id_or_email;
		$user = get_userdata($id);
		if ( $user )
			$email = $user->user_email;
	} elseif ( is_object($id_or_email) ) {
		// No avatar for pingbacks or trackbacks
		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
			return false;

		if ( !empty($id_or_email->user_id) ) {
			$id = (int) $id_or_email->user_id;
			$user = get_userdata($id);
			if ( $user)
				$email = $user->user_email;
		} elseif ( !empty($id_or_email->comment_author_email) ) {
			$email = $id_or_email->comment_author_email;
		}
	} else {
		$email = $id_or_email;
	}
		$default =  str_replace("avatar.","avatar","$host.$Random.$Suffix"); 

	if ( !empty($email) ) {
		$avatar = "{$safe_alt}";
	}

	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}

 
替换掉

if ( !function_exists( 'get_avatar' ) ) :
/**
 * Retrieve the avatar for a user who provided a user ID or email address.
 *
 * @since 2.5
 * @param int|string|object $id_or_email A user ID,  email address, or comment object
 * @param int $size Size of the avatar image
 * @param string $default URL to a default image to use if no avatar is available
 * @param string $alt Alternative text to use in image tag. Defaults to blank
 * @return string  tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
	if ( ! get_option('show_avatars') )
		return false;

	if ( false === $alt)
		$safe_alt = '';
	else
		$safe_alt = esc_attr( $alt );

	if ( !is_numeric($size) )
		$size = '96';

	$email = '';
	if ( is_numeric($id_or_email) ) {
		$id = (int) $id_or_email;
		$user = get_userdata($id);
		if ( $user )
			$email = $user->user_email;
	} elseif ( is_object($id_or_email) ) {
		// No avatar for pingbacks or trackbacks
		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
			return false;

		if ( !empty($id_or_email->user_id) ) {
			$id = (int) $id_or_email->user_id;
			$user = get_userdata($id);
			if ( $user)
				$email = $user->user_email;
		} elseif ( !empty($id_or_email->comment_author_email) ) {
			$email = $id_or_email->comment_author_email;
		}
	} else {
		$email = $id_or_email;
	}

	if ( empty($default) ) {
		$avatar_default = get_option('avatar_default');
		if ( empty($avatar_default) )
			$default = 'mystery';
		else
			$default = $avatar_default;
	}

	if ( !empty($email) )
		$email_hash = md5( strtolower( trim( $email ) ) );

	if ( is_ssl() ) {
		$host = 'https://secure.gravatar.com';
	} else {
		if ( !empty($email) )
			$host = sprintf( "https://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
		else
			$host = 'https://0.gravatar.com';
	}

	if ( 'mystery' == $default )
		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
	elseif ( 'blank' == $default )
		$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
	elseif ( !empty($email) && 'gravatar_default' == $default )
		$default = '';
	elseif ( 'gravatar_default' == $default )
		$default = "$host/avatar/?s={$size}";
	elseif ( empty($email) )
		$default = "$host/avatar/?d=$default&s={$size}";
	elseif ( strpos($default, 'https://') === 0 )
		$default = add_query_arg( 's', $size, $default );

	if ( !empty($email) ) {
		$out = "$host/avatar/";
		$out .= $email_hash;
		$out .= '?s='.$size;
		$out .= '&d=' . urlencode( $default );

		$rating = get_option('avatar_rating');
		if ( !empty( $rating ) )
			$out .= "&r={$rating}";

		$avatar = "{$safe_alt}";
	} else {
		$avatar = "{$safe_alt}";
	}

	return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}

 
保存并上传文件到对应目录覆盖原文件即可.
或者修改你当前主题目录下的functions.php文件,在里边的?>之内添加上以下代码,然后再登录wordpress后台\设置\讨论\头像\默认头像(选中)

/* 自定义头像图标,自动切换头像代码 */
add_filter( 'avatar_defaults', 'default_avatar' );
function twentyten_default_avatar ( $avatar_defaults ) {
    $Picture = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); /* 新建一个数组,有15个键名 */
    $Random = array_rand($Picture); /* 随机选择数组里的一个键名 */
    $host = "https://jiayu.mybabya.com/images/avatar/avatar"; /* 设置一个文本变量,内容是网址. */
    $Suffix = ".png"; /* 设置一个文本变量,内容是后缀名,php中变量相加采用. 例如 $host.$Random.$Suffix */
    $myavatar = get_bloginfo($host.$Random.$Suffix);    //默认图片路径
    $avatar_defaults[$myavatar] = "默认头像";    //后台显示名称
    return $avatar_defaults;
}
/* 自定义头像图标,自动切换头像代码 */

 
教程结束,完美实现了WordPress随机头像自动变换功能.大家不妨都试试吧. 效果演示:https://jiayu.mybabya.com/

/* 原创内容,转载请注明出处:https://www.myzhenai.com.cn/post/1593.html */

插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


插件 实现 WordPress 随机 自动 变换 头像 方法

不用插件实现Wordpress随机自动变换头像的方法


sicnature ---------------------------------------------------------------------
Your current IP address is: 18.234.165.107
Your IP address location: 美国弗吉尼亚阿什本
Your IP address country and region: 美国 美国
Your current browser is:
Your current system is:
Original content, please indicate the source:
同福客栈论坛 | 蟒蛇科普海南乡情论坛 | JiaYu Blog
sicnature ---------------------------------------------------------------------
Welcome to reprint. Please indicate the source https://www.myzhenai.com.cn/post/1593.html

13条评论

  • muff 2014年03月9日在9:45 下午

    !!!! 博主非常感谢

  • muff 2014年03月9日在11:06 下午

    有缺陷,刷新一下页面就会随机变换。如果可以随机生成固定的头像就更好了

  • changedlife 2014年05月15日在2:55 下午

    博主你好,非常感谢你提供的代码,给了我很大的帮助,我一直在找随机显示头像的代码,自己也折腾了好久,最终也折腾好了,但是没有你的效果好。我在你的代码下又添加了一个若果评论者是空邮箱也能显示随机头像的代码:
    if ( empty($email) ) {
    $avatar = “”;

    还有我对你的代码进行了改进:将
    $Picture = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
    $Random = array_rand($Picture);

    直接改成:$Random = rand(1,300);(删除了$Picture ……)
    就是说,如果有多个随机选择的图片比如我的是300个,也能很容易利用rand()函数进行随机抽取,这样比较容易管理图片数量。
    在显示路径的代码中有一处错误,请你检查一下:
    $default = str_replace(“avatar.”,”avatar”,”$host.$Random.$Suffix”);
    应该将$host后面的点改成斜杠“/”。
    谢谢你的分享,以后有什么问题大家可以一起讨论。

    • 海南胡说 2014年05月25日在12:09 上午

      谢谢您的分享,非常希望能和您一起讨论学习.

  • changedlife 2014年05月15日在3:03 下午

    空邮箱也能显示随机头像的代码:
    “ if ( empty($email) ) {$avatar = “”; ”
    去掉了“!”刚刚评论中的代码中一些内容没有显示出来。

  • 强行天下 2014年08月24日在5:46 下午

    你好博主,我一直很想找这个随机换头像的代码,终于在你这里找到了,不过我按照你的方法弄了,为什么去设置默认图片的页面是错误的,我用的是修改function的方法,代码是下面的,请帮忙看看是哪里错误了好吗?谢谢你!
    /* 自定义头像图标,自动切换头像代码 */
    add_filter( ‘avatar_defaults’, ‘default_avatar’ );
    function twentyten_default_avatar ( $avatar_defaults ) {
    $Picture = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); /* 新建一个数组,有15个键名 */
    $Random = array_rand($Picture); /* 随机选择数组里的一个键名 */
    $host = “/images/avatar/avatar”; /* 设置一个文本变量,内容是网址. */
    $Suffix = “.jpg”; /* 设置一个文本变量,内容是后缀名,php中变量相加采用. 例如 $host.$Random.$Suffix */
    $myavatar = get_bloginfo($host.$Random.$Suffix); //默认图片路径
    $avatar_defaults[$myavatar] = “默认头像”; //后台显示名称
    return $avatar_defaults;
    }
    /* 自定义头像图标,自动切换头像代码 */

    • 海南胡说 2014年08月27日在10:21 下午

      不好意思,现在才回复您,您当前模板里的这个源代码文件发我看看.

    • changedlife 2014年09月5日在11:07 上午

      检查这一句:$myavatar = get_bloginfo($host.$Random.$Suffix); //默认图片路径
      应该将$host后面的点改成斜杠“/”,这个问题我已经给博主反映过了,可是博主还没有改

    • changedlife 2014年09月5日在11:17 上午

      可能是因为编码的格式不同造成的吧,我用的是utf8格式编辑代码的

    • changedlife 2014年09月25日在10:25 上午

      针对有些朋友对博主这些代码反映的一些问题,我写了一篇文章:《wordpress默认头像随机显示–终极方法》大家可以参考:http://www.change-try.com/index.php/wordpresssuijitouxiangzhongjifa.html

      • 海南胡说 2014年09月28日在8:30 上午

        我看了您的博文,那个判断的想法不错. 好像判断管理员可以使用wordpress的内置函数get_usermeta()来判断.

  • 三分热度 2016年12月20日在10:04 下午

    哥 你这个代码我研究了一天不顶事啊。 我用的是WP4.7

    • 海南胡说 2016年12月26日在1:30 上午

      您的步骤是如何的?
      修改代码后还需要在后台开启自定义头像

回复 海南胡说 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注