set a user-meta key as avatar

Question

i have a user-meta kay named "avatar";
This value contains the URL of the image that the user upload.
how i can to set this meta value as user avatar?
i use this code and not work

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, '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;

}
0
mr. m0hmam4d 2 months 0 Answers 11 views 0

Leave an answer