0

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( 'anonimus_0@anonimus.com', 'anonimus_1@anonimus.com', 'anonimus_2@anonimus.com', 'anonimus_3@anonimus.com', 'anonimus_4@anonimus.com', 'anonimus_5@anonimus.com', 'anonimus_6@anonimus.com', 'anonimus_7@anonimus.com', 'anonimus_8@anonimus.com', 'anonimus_9@anonimus.com', 'anonimus_10@anonimus.com', 'anonimus_11@anonimus.com', 'anonimus_12@anonimus.com', 'anonimus_13@anonimus.com', 'anonimus_14@anonimus.com', 'anonimus_15@anonimus.com', 'anonimus_16@anonimus.com', 'anonimus_17@anonimus.com', 'anonimus_18@anonimus.com', 'anonimus_19@anonimus.com', 'anonimus_20@anonimus.com', 'anonimus_21@anonimus.com', 'anonimus_22@anonimus.com', 'anonimus_23@anonimus.com', 'anonimus_24@anonimus.com', 'anonimus_25@anonimus.com', 'anonimus_26@anonimus.com', 'anonimus_27@anonimus.com', 'anonimus_28@anonimus.com', 'anonimus_29@anonimus.com', 'anonimus_30@anonimus.com', 'anonimus_31@anonimus.com', 'anonimus_32@anonimus.com', 'anonimus_33@anonimus.com', 'anonimus_34@anonimus.com', 'anonimus_35@anonimus.com', 'anonimus_36@anonimus.com', 'anonimus_37@anonimus.com', 'anonimus_38@anonimus.com', 'anonimus_39@anonimus.com', 'anonimus_40@anonimus.com', 'anonimus_41@anonimus.com', 'anonimus_42@anonimus.com', 'anonimus_43@anonimus.com', 'anonimus_44@anonimus.com', 'anonimus_45@anonimus.com', 'anonimus_46@anonimus.com', 'anonimus_47@anonimus.com', 'anonimus_48@anonimus.com', 'anonimus_49@anonimus.com', 'anonimus_50@anonimus.com', 'anonimus_51@anonimus.com', 'anonimus_52@anonimus.com', 'anonimus_53@anonimus.com', 'anonimus_54@anonimus.com', 'anonimus_55@anonimus.com', 'anonimus_56@anonimus.com', 'anonimus_57@anonimus.com', 'anonimus_58@anonimus.com', 'anonimus_59@anonimus.com');
    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

1 Answer 1

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_\d+@anonimus\.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.

7
  • Sally CJ Thank! It works. And please help to apply this to AMP pages too. filter ampforwp_get_comments_gravatar Jul 28, 2020 at 8:11
  • Please check the revised answer and try all the code (but remove the previous code I provided in the original answer).
    – Sally CJ
    Jul 28, 2020 at 11:05
  • You are the best! :-) Jul 29, 2020 at 5:03
  • 1
    Just noticed, appears when opening AMP pages Undefined variable: email in ...functions.php on line 37 Problem this if ( $email && preg_match( '/^anonimus_\d+@anonimus\.com$/', $email ) ) { Jul 29, 2020 at 9:29
  • 1
    Perfect now. Many thanks! Jul 30, 2020 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.