0

In this post, explains how to make my question. It filters user_avatar. I use same way with filtering user_avatar_url for using in <img src="">

Here is my code:

function replaceAvatar( $url, $id_or_email, $args ) {

    // 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 $url;
    }

    // Get user meta
    $meta = get_user_meta( $id_or_email, 'ab_userAvatar', true );
    if( filter_var( $meta, FILTER_VALIDATE_URL ) ) {
        return $meta;
    }

    return $url;
}

add_filter( 'get_avatar_url', 'replaceAvatar', 10, 3 );

It works good when i call like this: get_avatar_url( $user->ID, array( 'size' => 120 ) )

But i got this following error in default wordpress comment list:

Warning: strlen() expects parameter 1 to be string, object given in /home/my_theme/public_html/wp-includes/formatting.php on line 2891

Warning: strlen() expects parameter 1 to be string, object given in /home/my_theme/public_html/wp-includes/formatting.php on line 2891

Your Answer

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

Browse other questions tagged or ask your own question.