Change User Avatar Programmatically in Wordpress

前端 未结 3 1739
说谎
说谎 2021-01-14 00:55

Is it possible to change the user avatar in WordPress programmatically? I\'m asking because I\'m facing a problem right now in displaying the user avatar in WordPress multis

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 01:14

    This will works :

    add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
    
    function ht1_change_avatar($args, $id_or_email) {
        if($id_or_email == 1) {
            $args['url'] = 'https://uinames.com/api/photos/female/1.jpg';
        }
    
        if($id_or_email == 2) {
            $args['url'] = 'https://uinames.com/api/photos/male/19.jpg';
        }
    
        return $args;
    } // end of function
    

    If you have avatar dir location metas for users, then use this for all users :

    add_filter('get_avatar_data', 'ht1_change_avatar', 100, 2);
    
    function ht1_change_avatar($args, $id_or_email) {
        $avatar_url = get_user_meta($id_or_email, 'avatar', true);
    
        $args['url'] = $avatar_url;
    
        return $args;
    } // end of function
    

    Hope you get the point.

提交回复
热议问题