是否可以通過編程方式更改WordPress中的用戶頭像?我問,因爲我現在在使用WordPress多站點顯示用戶頭像時遇到了問題:頭像未顯示。在Wordpress中以編程方式更改用戶頭像
2
A
回答
1
最有可能的地方get_avatar
過濾器被稱爲和做某事。我建議搜索你的插件和主題get_avatar
,看看看起來像這樣的東西:add_filter ('get_avatar', .....
否則你可以用下面的代碼編寫自己的行爲。
<?php // in a plugin file or in a theme functions.php
function SO13911452_override_avatar ($avatar_html, $id_or_email, $size, $default, $alt) {
// check all values
return $avatar_html
}
add_filter ('get_avatar', 'SO13911452_override_avatar', 10, 5);
0
我必須做的三件事情是能夠以編程方式插入用戶頭像進入我的WordPress開始與在遠程URL託管的化身。
- 安裝WP User Avatar插件。
- 借用WooCommerce的上傳功能。見下文。
- 從類似support post
適應一些代碼,假設你有其頭像是$avatar_url = 'http://cdn.sstatic.net/stackoverflow/img/[email protected]?v=73d79a89bded&a';
我使用upload_product_image()
從WooCommerce的class-wc-api-products.php
得到頭像進入我的本地服務器的用戶。
然後,使用support post中的一些代碼創建一個附件。 然後將附件與用戶關聯起來。
這隻適用於WP用戶頭像插件。
function se13911452_set_avatar_url($avatar_url, $user_id) {
global $wpdb;
$file = upload_product_image($avatar_url);
$wp_filetype = wp_check_filetype($file['file']);
$attachment = array(
'guid' => $file['url'],
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file['file']);
$attach_data = wp_generate_attachment_metadata($attach_id, $file['file']);
wp_update_attachment_metadata($attach_id, $attach_data);
update_user_meta($user_id, $wpdb->get_blog_prefix() . 'user_avatar', $attach_id);
}
從WooCommerce'sclass-wc-api-products.php
/**
* WooCommerce class-wc-api-products.php
* See https://github.com/justinshreve/woocommerce/blob/master/includes/api/class-wc-api-products.php
* Upload image from URL
*
* @since 2.2
* @param string $image_url
* @return int|WP_Error attachment id
*/
function upload_product_image($image_url) {
$file_name = basename(current(explode('?', $image_url)));
$wp_filetype = wp_check_filetype($file_name, null);
$parsed_url = @parse_url($image_url);
// Check parsed URL
if(!$parsed_url || !is_array($parsed_url)) {
throw new WC_API_Exception('woocommerce_api_invalid_product_image', sprintf(__('Invalid URL %s', 'woocommerce'), $image_url), 400);
}
// Ensure url is valid
$image_url = str_replace(' ', '%20', $image_url);
// Get the file
$response = wp_safe_remote_get($image_url, array(
'timeout' => 10
));
if(is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {
throw new WC_API_Exception('woocommerce_api_invalid_remote_product_image', sprintf(__('Error getting remote image %s', 'woocommerce'), $image_url), 400);
}
// Ensure we have a file name and type
if(!$wp_filetype['type']) {
$headers = wp_remote_retrieve_headers($response);
if(isset($headers['content-disposition']) && strstr($headers['content-disposition'], 'filename=')) {
$disposition = end(explode('filename=', $headers['content-disposition']));
$disposition = sanitize_file_name($disposition);
$file_name = $disposition;
}
elseif(isset($headers['content-type']) && strstr($headers['content-type'], 'image/')) {
$file_name = 'image.' . str_replace('image/', '', $headers['content-type']);
}
unset($headers);
}
// Upload the file
$upload = wp_upload_bits($file_name, '', wp_remote_retrieve_body($response));
if($upload['error']) {
throw new WC_API_Exception('woocommerce_api_product_image_upload_error', $upload['error'], 400);
}
// Get filesize
$filesize = filesize($upload['file']);
if(0 == $filesize) {
@unlink($upload['file']);
unset($upload);
throw new WC_API_Exception('woocommerce_api_product_image_upload_file_error', __('Zero size file downloaded', 'woocommerce'), 400);
}
unset($response);
return $upload;
}
相關問題
- 1. Drupal 7以編程方式設置用戶頭像(頭像)
- 2. 以編程方式更改圖像
- 3. 以編程方式更改圖像源
- 4. Wordpress,以編程方式創建用戶
- 5. 以編程方式在android中更改攝像頭分辨率設置
- 6. 如何以編程方式更改Wordpress中的菜單?
- 7. 如何在viewpager中以編程方式更改圖像?
- 8. 以編程方式在Fresco中更改SimpleDraweeView的背景圖像
- 9. 以編程方式更改FormsAuthenticationTicket中的用戶數據
- 10. 無法在wordpress環境中以編程方式創建用戶
- 11. 以編程方式更改標籤方向,編程方式
- 12. mgwt - 以編程方式更改方向
- 13. 更改用戶頭像
- 14. 如何以編程方式更改Wordpress上傳文件夾?
- 15. 如何以編程方式用「viewWithTag」更改圖像?
- 16. 如何以編程方式更改Corona中顯示的圖像?
- 17. 如何以編程方式更改圖像中的顏色?
- 18. 以編程方式更改創建Drupal用戶名
- 19. MapKit以編程方式更改用戶位置
- 20. 如何以編程方式更改用戶權限?
- 21. Drupal 6:以編程方式更改用戶值
- 22. Oracle數據庫以編程方式更改用戶密碼
- 23. 以編程方式在wordpress上創建用戶
- 24. 在Windows下以編程方式禁用網絡攝像頭
- 25. 如何以編程方式在Twitter中設置我的頭像?
- 26. 以編程方式在android中打開前置攝像頭
- 27. 以編程方式設置圖像視圖,而用戶看不到它更改?
- 28. 以編程方式更改ImageResource(繪製)
- 29. 以編程方式更改膚色
- 30. xaml - 以編程方式更改StandardStyles
你能給更多的信息?你使用默認的內置gravatar嗎?他們在哪裏不顯示?運行哪些代碼來顯示它們? – janw
是的我使用WordPress中默認的內置gravatar。它們不會顯示在用戶在主博客中創建的新網站(博客)上。我使用get_avatar()來顯示。 – rjx44
您可以試試https://wordpress.org/support/topic/setting-users-avatar-programmatically – Kirby