問題描述
我正在使用一個名為 Simple Local Avatars 的外掛,可以讓我上傳本地儲存在我的伺服器上的作者影像 (沒有 Gravatar) 。該外掛工作正常,get_avatar 返回本地頭像。
但是,我需要以不同的方式和不同的地方使用該頭像,因此我需要本地的頭像影像 URL,而不是整個 HTML 標籤。我可以為 get_avatar 編寫一個包裝函式,它使用 RegEx 或 SimpleXML 來選擇並返回 URL,但是我想知道是否有現有的方法來做到這一點。
最佳解決方案
WordPress 4.2 版本的好訊息
從 4.2 版本開始,便捷的 get_avatar_url()功能,幾年前在 #21195 機票中引入了功能要求,now ships with the core:
/**
* Retrieve the avatar URL.
*
* @since 4.2.0
*
* @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
* user email, WP_User object, WP_Post object, or comment object.
* @param array $args {
* Optional. Arguments to return instead of the default arguments.
*
* @type int $size Height and width of the avatar in pixels. Default 96.
* @type string $default URL for the default image or a default type. Accepts '404' (return
* a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
* 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
* or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
* 'gravatar_default' (the Gravatar logo). Default is the value of the
* 'avatar_default' option, with a fallback of 'mystery'.
* @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
* @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
* judged in that order. Default is the value of the 'avatar_rating' option.
* @type string $scheme URL scheme to use. See set_url_scheme() for accepted values.
* Default null.
* @type array $processed_args When the function returns, the value will be the processed/sanitized $args
* plus a "found_avatar" guess. Pass as a reference. Default null.
* }
* @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
*/
function get_avatar_url( $id_or_email, $args = null ) {
$args = get_avatar_data( $id_or_email, $args );
return $args['url'];
}
其中 get_avatar_data()也是一個新的幫助函式。
它包含這個程式碼部分:
... CUT ...
/**
* Filter whether to retrieve the avatar URL early.
*
* Passing a non-null value in the 'url' member of the return array will
* effectively short circuit get_avatar_data(), passing the value through
* the {@see 'get_avatar_data'} filter and returning early.
*
* @since 4.2.0
*
* @param array $args Arguments passed to get_avatar_data(), after processing.
* @param int|object|string $id_or_email A user ID, email address, or comment object.
*/
$args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
/** This filter is documented in wp-includes/link-template.php */
return apply_filters( 'get_avatar_data', $args, $id_or_email );
}
... CUT ...
我們可以看到,當設定 url 引數時,可用的過濾器是 pre_get_avatar_data 和 get_avatar_data 。
在最近升級到 4.2 之後,我遇到了一個主題的問題,它定義了它自己的 get_avatar_url()版本,沒有任何函式名稱字首或 function_exists()檢查。所以這是一個為什麼這很重要的例子;-)
次佳解決方案
上面的答案似乎是全面的,但我剛剛寫了一個包裝函式並繼續。這就是如果你需要它 (把它放在 functions.php):
function get_avatar_url($get_avatar){
preg_match("/src='(.*?)'/i", $get_avatar, $matches);
return $matches[1];
}
然後在這樣的模板檔案中使用 wherver 你需要它:
<img src="<? echo get_avatar_url(get_avatar( $curauth->ID, 150 )); ?>" align="left" class="authorimage" />
這更簡單
在這種情況下使用 RegEx 解析 HTML 可以,因為這隻能解析一個 img 標籤,所以不會太貴。
第三種解決方案
您可以使用過濾器 get_avatar 獲取頭像中的所有資料,也可以使用標記內的 URL 。我想,WP 不具有返回只有 url 的功能,如果頭像影像。
$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
此外,您可以在外掛或主題中重寫此函式,如果此函式名稱未在其他位置定義,該函式將為 onyl 活動。
if ( ! function_exists( 'get_avatar' ) ) :
所以可以新增一個 param 來返回影像的 URL,像這樣,使用引數 $url 與 TRUE,你只得到 url 。
/**
* 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 Alternate text to use in image tag. Defaults to blank
* @param boolean $url, true for get only the url of the image, no markup
* @return string <img> tag for the user's avatar
*/
function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false, $url = 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( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
else
$host = 'http://0.gravatar.com';
}
if ( 'mystery' == $default )
$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
elseif ( 'blank' == $default )
$default = 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, 'http://') === 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}";
if ( $url )
$avatar = $out;
else
$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
} else {
if ( $url )
$avatar = $out;
else
$avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
}
return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
}
另一個小小的變體是,您建立了 Gravatar 規則的 URL 。
function get_gravatar_url( $email ) {
$hash = md5( strtolower( trim ( $email ) ) );
return 'http://gravatar.com/avatar/' . $hash;
}
使用這個在您的來源與作者的電子郵件,你得到的影像的 url 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。