admin管理员组文章数量:1130349
I've added functionality to upload profile picture by following THIS guide.
I can't find an online guide or any documentation about WP hooks.. How to replace Gravatar profile pictures (in comment section) with custom uploaded images?
I don't want to force my users to register Gravatar in order to change their profile picture on my site.
I've added functionality to upload profile picture by following THIS guide.
I can't find an online guide or any documentation about WP hooks.. How to replace Gravatar profile pictures (in comment section) with custom uploaded images?
I don't want to force my users to register Gravatar in order to change their profile picture on my site.
Share Improve this question edited Nov 23, 2015 at 21:15 N00b asked Nov 23, 2015 at 5:14 N00bN00b 2,1794 gold badges22 silver badges32 bronze badges 6 | Show 1 more comment4 Answers
Reset to default -3If you are set your custom or uploaded profile picture and need to see on front end, you can use below function .
<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>
If you have to change your gravatar to custom profile picture you can refer below link : http://www.wpbeginner/wp-tutorials/how-to-change-the-default-gravatar-on-wordpress/
Presuming that the user has their avatar saved, as the ID of an attachment, store in user meta, as the field field_with_custom_avatar_id, you could do this to show that attachment if the value is saved:
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find ID of attachment saved user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar_id', true );
if( 0 < absint( $saved ) ) {
//return saved image
return wp_get_attachment_image( $saved, [ $size, $size ], false, ['alt' => $alt] );
}
//return normal
return $avatar;
}
Or, if it is saved as a URL of the image, in the user meta field field_with_custom_avatar -
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar', true );
//check if it is a URL
if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
//return saved image
return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
}
//return normal
return $avatar;
}
The hook you need is the get_avatar filter. It returs the image HTML element representing the user avatar.
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
return $avatar;
}
Note that using this filter, you can still let users use gravatar. You could check if the user has uploaded an avatar to your site, then use it, it not you return normal $avatar, which will be from gravatar if user has one. (If you add to question the code you are using to store user avatars, I can give a exact working code):
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
if( "user_has_uploaded_a_local_avatar" ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
}
// if user didn't upload a local avatar,
// normal avatar will be used, which can be from gravatar
return $avatar;
}
This is a "me too" comment but with a solution :)
So when I enabled comments section I got an error from is_email($id_or_email).
Here's the error
strlen() expects parameter 1 to be string, object given in /home/my_theme/public_html/wp-includes/formatting.php on line 2891
Error happens, cause $id_or_email contains actually an object and not a string.
I found a workaround by fetching email string from the object $id_or_email->comment_author_email
So I changed $id_or_email to $id_or_email->comment_author_email and now I get the right avatar picture to the comments and no errors.
I've added functionality to upload profile picture by following THIS guide.
I can't find an online guide or any documentation about WP hooks.. How to replace Gravatar profile pictures (in comment section) with custom uploaded images?
I don't want to force my users to register Gravatar in order to change their profile picture on my site.
I've added functionality to upload profile picture by following THIS guide.
I can't find an online guide or any documentation about WP hooks.. How to replace Gravatar profile pictures (in comment section) with custom uploaded images?
I don't want to force my users to register Gravatar in order to change their profile picture on my site.
Share Improve this question edited Nov 23, 2015 at 21:15 N00b asked Nov 23, 2015 at 5:14 N00bN00b 2,1794 gold badges22 silver badges32 bronze badges 6- Have you considered Buddypress? – Dan Alexander Commented Nov 23, 2015 at 6:38
- so you prefer to force them potentially to upload an image on every site? I am not a fan of gravatar but this is exactly the one (and only) problem that it solves. – Mark Kaplun Commented Nov 23, 2015 at 8:37
-
@MarkKaplun using
get_avatarfilter, users can still use gravatar. See my answer. I usually don't recomend third party plugins, even less if it has not been update for a long time, but local simple avatar does a good job and it also adds audience ratings to local avatars given a complete solution. It can be a very good starting point for your own plugin. – cybmeta Commented Nov 23, 2015 at 10:05 - @cybmeta, sure it is possible and useful, I just question the logic of doing it in the context of the question. It is probably easier to get an image on gravater than whatever @ Noob will use as his implementation. If the reason is not related to privacy or moderation then I don't see how a local solution can be better then gravatar. To me today it makes more sense to let the user use his FB, twitter or google avatar if your main concern is just user convenience.. – Mark Kaplun Commented Nov 23, 2015 at 10:19
- 1 No one said it is better ;). Personally I like the idea to not force your users to use a third party service, gravatar, facebook or any other, if they want to have a full experience in your site. Of course using popular social networks services can be a gain for your site, but offer a full experience to your users also. It is not better or worst, just a choice that we can take as owner of the website. And the context of the question was not to force to upload local avatars, it was the opposite, to not force to use gravatar (it can be extended to not force the use of a third party service). – cybmeta Commented Nov 23, 2015 at 10:23
4 Answers
Reset to default -3If you are set your custom or uploaded profile picture and need to see on front end, you can use below function .
<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>
If you have to change your gravatar to custom profile picture you can refer below link : http://www.wpbeginner/wp-tutorials/how-to-change-the-default-gravatar-on-wordpress/
Presuming that the user has their avatar saved, as the ID of an attachment, store in user meta, as the field field_with_custom_avatar_id, you could do this to show that attachment if the value is saved:
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find ID of attachment saved user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar_id', true );
if( 0 < absint( $saved ) ) {
//return saved image
return wp_get_attachment_image( $saved, [ $size, $size ], false, ['alt' => $alt] );
}
//return normal
return $avatar;
}
Or, if it is saved as a URL of the image, in the user meta field field_with_custom_avatar -
add_filter( 'get_avatar', 'slug_get_avatar', 10, 5 );
function slug_get_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
//If is email, try and find user ID
if( ! is_numeric( $id_or_email ) && is_email( $id_or_email ) ){
$user = get_user_by( 'email', $id_or_email );
if( $user ){
$id_or_email = $user->ID;
}
}
//if not user ID, return
if( ! is_numeric( $id_or_email ) ){
return $avatar;
}
//Find URL of saved avatar in user meta
$saved = get_user_meta( $id_or_email, 'field_with_custom_avatar', true );
//check if it is a URL
if( filter_var( $saved, FILTER_VALIDATE_URL ) ) {
//return saved image
return sprintf( '<img src="%" alt="%" />', esc_url( $saved ), esc_attr( $alt ) );
}
//return normal
return $avatar;
}
The hook you need is the get_avatar filter. It returs the image HTML element representing the user avatar.
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
return $avatar;
}
Note that using this filter, you can still let users use gravatar. You could check if the user has uploaded an avatar to your site, then use it, it not you return normal $avatar, which will be from gravatar if user has one. (If you add to question the code you are using to store user avatars, I can give a exact working code):
add_filter( 'get_avatar', 'cyb_get_avatar', 10, 5 );
function cyb_get_avatar( $avatar = '', $id_or_email, $size = 96, $default = '', $alt = '' ) {
if( "user_has_uploaded_a_local_avatar" ) {
// Replace $avatar with your own image element, for example
// $avatar = "<img alt='$alt' src='your_new_avatar_url' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"
}
// if user didn't upload a local avatar,
// normal avatar will be used, which can be from gravatar
return $avatar;
}
This is a "me too" comment but with a solution :)
So when I enabled comments section I got an error from is_email($id_or_email).
Here's the error
strlen() expects parameter 1 to be string, object given in /home/my_theme/public_html/wp-includes/formatting.php on line 2891
Error happens, cause $id_or_email contains actually an object and not a string.
I found a workaround by fetching email string from the object $id_or_email->comment_author_email
So I changed $id_or_email to $id_or_email->comment_author_email and now I get the right avatar picture to the comments and no errors.
本文标签: profilesReplace Gravatar with uploaded images
版权声明:本文标题:profiles - Replace Gravatar with uploaded images? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749031380a2305731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


get_avatarfilter, users can still use gravatar. See my answer. I usually don't recomend third party plugins, even less if it has not been update for a long time, but local simple avatar does a good job and it also adds audience ratings to local avatars given a complete solution. It can be a very good starting point for your own plugin. – cybmeta Commented Nov 23, 2015 at 10:05