最近在更新一個主題的時候也加入了該功能效果,實現的方法是通過加載 javascript 的 md5 函數轉換捕獲的郵箱輸入框的輸入的 email 地址,然後組合成對應的 gravatar 頭像鏈接,最後用 setAttribute 改變 img 的 src 地址。

實現WordPress評論輸入郵箱即時顯示gravatar頭像

操作步驟:

1 、下載 js 文件,並上傳至當前企業網站模板主題文件夾

js-md5-gravatar.zip

2 、編輯當前主題的 comments.php 文件,在要即時顯示頭像對應的文件位置添加代碼:

<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #000088;">$email <span%20style="color: #339933;">= <span%20style="color: #009900;">(<span%20style="color: #000088;">$user_ID<span%20style="color: #009900;">) ? get_the_author_meta<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'user_email'<span%20style="color: #339933;">, <span%20style="color: #000088;">$user_ID<span%20style="color: #009900;">) <span%20style="color: #339933;">: <span%20style="color: #000088;">$comment_author_email<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?><img id="real-time-gravatar" src="http://www.gravatar.com/avatar/<span%20style="color: #000000; font-weight: bold;"><?php <span%20style="color: #b1b100;">echo <span%20style="color: #990000;">md5<span%20style="color: #009900;">(<span%20style="color: #000088;">$email<span%20style="color: #009900;">)<span%20style="color: #339933;">;<span%20style="color: #000000; font-weight: bold;">?>?s=60&d=identicon&r=G" alt="gravatar 頭像" />

<span%20style="color: #ff0000;"> 注:代碼中的 60 表示圖像圖片的大小

3 、在 comments.php 文件第一行添加以下調用代碼:

<script type="text/javascript" src="<span%20style="color: #000000; font-weight: bold;"><?php bloginfo<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'template_url'<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>js-md5.js"></script><script type="text/javascript" src="<span%20style="color: #000000; font-weight: bold;"><?php bloginfo<span%20style="color: #009900;">(<span%20style="color: #0000ff;">'template_url'<span%20style="color: #009900;">)<span%20style="color: #339933;">; <span%20style="color: #000000; font-weight: bold;">?>/gravatar.js"></script>

<span%20style="color: #ff0000;"> 注:如果 js 文件路徑有改動,請修改相應的路徑位置

保存文件,刷新網頁後,測試成功。