admin管理员组文章数量:1328923
My code not work :-(
(numbers in e-mails 0, 1, 2, 3... 59 - these are seconds)
add_filter('get_avatar', 'my_get_avatar', 10, 5);
function my_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
$mail = $id_or_email->comment_author_email;
$email_list = array( '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
if (array_search($mail, $email_list) == true ) {
$img = ".png";
$avatar = "<img src='".$img ."' alt='".$alt."' height='".$size."' width='".$size."' />";
}
return $avatar;
}
Sorry for my English
My code not work :-(
(numbers in e-mails 0, 1, 2, 3... 59 - these are seconds)
add_filter('get_avatar', 'my_get_avatar', 10, 5);
function my_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
$mail = $id_or_email->comment_author_email;
$email_list = array( '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]');
if (array_search($mail, $email_list) == true ) {
$img = "http://website/wp-content/anonimus-avatar.png";
$avatar = "<img src='".$img ."' alt='".$alt."' height='".$size."' width='".$size."' />";
}
return $avatar;
}
Sorry for my English
Share Improve this question asked Jul 27, 2020 at 22:53 UserUser-nameUserUser-name 577 bronze badges1 Answer
Reset to default 1Two things:
The
$id_or_email
can be an integer (a user ID), a string (Gravatar MD5 hash or user email) or an object (e.g. aWP_User
instance), so you can't simply do$mail = $id_or_email->comment_author_email;
.The email addresses in your list are all in the form of
anonimus_<number>@anonimus
, so instead of defining that long array, you can simply use regular expression (RegEx).
(Update) Plus another thing:
Instead of the
get_avatar
filter/hook, you should probably just use thepre_get_avatar_data
hook since in yourmy_get_avatar()
function, you're simply changing the avatar URL.That way (i.e. using the above hook), your callback would also work with
get_avatar_url()
and not justget_avatar()
.
So just remove the add_filter('get_avatar', 'my_get_avatar', 10, 5);
from your code (and remove also the my_get_avatar()
function), and then use the following instead:
add_filter( 'pre_get_avatar_data', 'my_pre_get_avatar_data', 10, 2 );
function my_pre_get_avatar_data( $args, $id_or_email ) {
// First, get the user's ID or email for unregistered comment authors.
if ( is_numeric( $id_or_email ) ) {
$user = get_user_by( 'id', $id_or_email );
} elseif ( $id_or_email instanceof WP_Comment ) {
$email = $id_or_email->comment_author_email;
$user = get_user_by( 'id', $id_or_email->user_id );
} elseif ( $id_or_email instanceof WP_Post ) {
$user = get_user_by( 'id', $id_or_email->post_author );
} elseif ( $id_or_email instanceof WP_User ) {
$user = get_user_by( 'id', $id_or_email->ID );
} else {
$user = get_user_by( 'email', $id_or_email );
}
// For registered users, get the email in the database.
if ( empty( $email ) && $user && ! is_wp_error( $user ) ) {
$email = $user->user_email;
}
// Then check the user's email and use the custom avatar, if applicable.
if ( ! empty( $email ) && preg_match( '/^anonimus_\d+@anonimus\$/', $email ) ) {
$args['url'] = 'http://example/wp-content/anonimus-avatar.png';
}
return $args;
}
But if you'd rather use the get_avatar
hook, then please let me know or just check this answer's revisions.
In response to your comment:
please help to apply this to AMP pages too. filter
ampforwp_get_comments_gravatar
You should really ask in the plugin support forums, but let's make an exception just this time, and you can try the following, which overrides the ampforwp_get_comments_gravatar()
function — yes, there's a hook with the same name, but I don't think it will work (because their code doesn't pass the $comment
variable to the hook callbacks).
if ( ! function_exists( 'ampforwp_get_comments_gravatar' ) ) :
function ampforwp_get_comments_gravatar( $comment ) {
global $redux_builder_amp;
if ( ! empty( $redux_builder_amp['ampforwp-display-avatar'] ) ) {
return get_avatar( $comment );
}
return '';
}
endif;
That is untested, but it should work. And if you need further customization help, please, ask/post another question.
本文标签: How to change users avatar with specific email addresses
版权声明:本文标题:How to change users avatar with specific e-mail addresses 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742222165a2435513.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论