How to change users avatar with specific e-mail addresses

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( '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f3e31303136322a2c006f1f3e31303136322a2c713c3032">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="99f8f7f6f7f0f4eceac6a8d9f8f7f6f7f0f4eceab7faf6f4">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06676869686f6b7375593446676869686f6b73752865696b">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55343b3a3b3c3820260a6615343b3a3b3c3820267b363a38">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="17767978797e7a6264482357767978797e7a62643974787a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="37565958595e5a4244680277565958595e5a42441954585a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="47262928292e2a3234187107262928292e2a32346924282a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82e3ecedecebeff7f1ddb5c2e3ecedecebeff7f1ace1edef">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a7a8a9a8afabb3b599fe86a7a8a9a8afabb3b5e8a5a9ab">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41202f2e2f282c34321e7801202f2e2f282c34326f222e2c">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afcec1c0c1c6c2dadcf09e9fefcec1c0c1c6c2dadc81ccc0c2">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="05646b6a6b6c6870765a343445646b6a6b6c6870762b666a68">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187976777671756d6b47292a587976777671756d6b367b7775">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88986878681859d9bb7d9dba88986878681859d9bc68b8785">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b2bdbcbdbabea6a08ce2e793b2bdbcbdbabea6a0fdb0bcbe">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f6f9f8f9fefae2e4c8a6a2d7f6f9f8f9fefae2e4b9f4f8fa">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98f9f6f7f6f1f5edebc7a9aed8f9f6f7f6f1f5edebb6fbf7f5">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d7c7372737470686e422c2a5d7c7372737470686e337e7270">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f4959a9b9a9d998187abc5ccb4959a9b9a9d998187da979b99">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a3b34353433372f29056b631a3b34353433372f2974393537">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5b54555453574f4965080a7a5b54555453574f4914595557">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97f6f9f8f9fefae2e4c8a5a6d7f6f9f8f9fefae2e4b9f4f8fa">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="583936373631352d2b076a6a183936373631352d2b763b3735">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c2cdcccdcaced6d0fc9190e3c2cdcccdcaced6d08dc0ccce">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="76171819181f1b030529444236171819181f1b03055815191b">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfded1d0d1d6d2cacce08d8affded1d0d1d6d2cacc91dcd0d2">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8eefe0e1e0e7e3fbfdd1bcb8ceefe0e1e0e7e3fbfda0ede1e3">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9a8a7a6a7a0a4bcba96fbfe89a8a7a6a7a0a4bcbae7aaa6a4">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71101f1e1f181c04022e434931101f1e1f181c04025f121e1c">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22434c4d4c4b4f57517d101b62434c4d4c4b4f57510c414d4f">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="badbd4d5d4d3d7cfc9e5898afadbd4d5d4d3d7cfc994d9d5d7">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0b1bebfbeb9bda5a38fe3e190b1bebfbeb9bda5a3feb3bfbd">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbdad5d4d5d2d6cec8e48889fbdad5d4d5d2d6cec895d8d4d6">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0a1aeafaea9adb5b39ff3f380a1aeafaea9adb5b3eea3afad">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="25444b4a4b4c4850567a161165444b4a4b4c4850560b464a48">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b3bcbdbcbbbfa7a18de1e792b3bcbdbcbbbfa7a1fcb1bdbf">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7969998999e9a8284a8c4c1b7969998999e9a8284d994989a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6c0d0203020501191f335f5b2c0d0203020501191f420f0301">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55343b3a3b3c3820260a666d15343b3a3b3c3820267b363a38">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bddcd3d2d3d4d0c8cee28e84fddcd3d2d3d4d0c8ce93ded2d0">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="adccc3c2c3c4c0d8def2999dedccc3c2c3c4c0d8de83cec2c0">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="04656a6b6a6d6971775b303544656a6b6a6d6971772a676b69">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a3acadacabafb7b19df6f082a3acadacabafb7b1eca1adaf">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5a4abaaabaca8b0b69af1f685a4abaaabaca8b0b6eba6aaa8">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8c8382838480989eb2d9d9ad8c8382838480989ec38e8280">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="96f7f8f9f8fffbe3e5c9a2a3d6f7f8f9f8fffbe3e5b8f5f9fb">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5c3d3233323531292f03686a1c3d3233323531292f723f3331">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85e4ebeaebece8f0f6dab1b2c5e4ebeaebece8f0f6abe6eae8">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3d2dddcdddadec6c0ec878bf3d2dddcdddadec6c09dd0dcde">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="51303f3e3f383c24220e656811303f3e3f383c24227f323e3c">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbeb1b0b1b6b2aaac80eaef9fbeb1b0b1b6b2aaacf1bcb0b2">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8c8382838480989eb2d8dcad8c8382838480989ec38e8280">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e88986878681859d9bb7dddaa88986878681859d9bc68b8785">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="37565958595e5a424468020477565958595e5a42441954585a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90f1fefffef9fde5e3cfa5a4d0f1fefffef9fde5e3bef3fffd">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e3f30313037332b2d016b6b1e3f30313037332b2d703d3133">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f7969998999e9a8284a8c2c1b7969998999e9a8284d994989a">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7e71707176726a6c402a285f7e71707176726a6c317c7072">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284946474641455d5b771d10684946474641455d5b064b4745">[email protected]</a>', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="680906070601051d1b375d51280906070601051d1b460b0705">[email protected]</a>');
    if (array_search($mail, $email_list) == true ) {
        $img = "http://website.com/wp-content/anonimus-avatar.png";
        $avatar = "<img src='".$img ."' alt='".$alt."' height='".$size."' width='".$size."' />";
    }
    return $avatar;
}

Sorry for my English

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Two things:

  1. The $id_or_email can be an integer (a user ID), a string (Gravatar MD5 hash or user email) or an object (e.g. a WP_User instance), so you can’t simply do $mail = $id_or_email->comment_author_email;.
  2. The email addresses in your list are all in the form of anonimus_<number>@anonimus.com, so instead of defining that long array, you can simply use regular expression (RegEx).

(Update) Plus another thing:

  1. Instead of the get_avatar filter/hook, you should probably just use the pre_get_avatar_data hook since in your my_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 just get_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_[email protected].com$/', $email ) ) {
        $args['url'] = 'http://example.com/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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x