WordPress 在用户未注册、未上传头像时,会用 get_avatar() 生成一个「神秘人」占位图(Gravatar 的默认图)。这个默认图非常丑,而且主题当前走 cn.gravatar.com,该域名在国内已超时 —— 结果是评论区右侧一片空白或破图。
get_avatar()
cn.gravatar.com
希望主题自带「自动生成头像」能力:按访客邮箱生成一张好看的头像,不需要访客注册、不需要外网请求。
用 DiceBear 的算法与素材,编译成纯 PHP 模板,零外网依赖。
@dicebear/core
createAvatar()
onPreCreate
getComponents
getColors
onPostCreate
只收录许可干净的 14 套(CC0 1.0 + MIT),其余 CC BY 4.0 需要署名,不适合随主题分发:
lorelei
open-peeps
notionists
notionists-neutral
lorelei-neutral
pixel-art
pixel-art-neutral
thumbs
shapes
glass
rings
identicon
initials
icons
合计约 960 KB。按需加载:只 require 用户选中的那一套,其余不解析。
require
关键坑(已实测):DiceBear 默认调色板基本全是单值(hairColor/eyesColor/mouthColor 都是 000000,skinColor 是 ffffff),不换调色板的话 30 个头像只有 2 种配色 —— 这才是「现在生成的特别丑」的真正原因,光换风格解决不了。所以必须内置人类调色板。
hairColor
eyesColor
mouthColor
000000
skinColor
ffffff
Requires PHP: 7.0
inc/theme-core.php
get_avatar
inc/theme-options.php
inc/theme-avatar.php
inc/avatar-styles/*.php
functions.php
3.2.35
3.2.36
/theme-update/
No dependencies set.
The note is not visible to the blocked user.
现象 / 诉求
WordPress 在用户未注册、未上传头像时,会用
get_avatar()生成一个「神秘人」占位图(Gravatar 的默认图)。这个默认图非常丑,而且主题当前走cn.gravatar.com,该域名在国内已超时 —— 结果是评论区右侧一片空白或破图。希望主题自带「自动生成头像」能力:按访客邮箱生成一张好看的头像,不需要访客注册、不需要外网请求。
需求细节(用户确认)
技术方案(已调研完毕,结论确定)
用 DiceBear 的算法与素材,编译成纯 PHP 模板,零外网依赖。
@dicebear/core的createAvatar():onPreCreate→getComponents→getColors→onPostCreate→ 背景层 → clip mask。只收录许可干净的 14 套(CC0 1.0 + MIT),其余 CC BY 4.0 需要署名,不适合随主题分发:
loreleiopen-peepsnotionistsnotionists-neutrallorelei-neutralpixel-artpixel-art-neutralthumbsshapesglassringsidenticoninitialsicons合计约 960 KB。按需加载:只
require用户选中的那一套,其余不解析。关键坑(已实测):DiceBear 默认调色板基本全是单值(
hairColor/eyesColor/mouthColor都是000000,skinColor是ffffff),不换调色板的话 30 个头像只有 2 种配色 —— 这才是「现在生成的特别丑」的真正原因,光换风格解决不了。所以必须内置人类调色板。验收标准
Requires PHP: 7.0基线不能被打破(不能用 PHP 8 语法)。get_avatar()。涉及范围(预估)
inc/theme-core.php:get_avatar过滤器接入点。inc/theme-options.php:新增选项定义。inc/theme-avatar.php:PRNG + 渲染器 +get_avatar钩子。inc/avatar-styles/*.php:14 套编译后的模板数据。functions.php:require新文件(按需)。备注
3.2.35→3.2.36。/theme-update/通道(zip + readme + update.json),不覆盖服务器主题目录,用户在 WP 后台自己升级。