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

/* 原创内容,转载请注明出处: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随机自动变换头像的方法

IP 地址 216.73.217.37
区域位置 美国加利福尼亚蒙诺维亚
系统信息 🇨🇳 系统 浏览器
最后修改:2013 年 10 月 24 日

赞赏支持

文章二维码