Change Avatar Programmatically
-
I am wanting to change the avatar of several of my users (ideally by their ID), is this possible.
Something like this – but this is not working for me
function set_avatar_url($avatar_url, $user_id) {
global $wpdb;
$file = upload_product_image($avatar_url);
$wp_filetype = wp_check_filetype($file[‘file’]);
$attachment = array(
‘guid’ => $file[‘url’],
‘post_mime_type’ => $wp_filetype[‘type’],
‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($file[‘file’])),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);
$attach_id = wp_insert_attachment($attachment, $file[‘file’]);
$attach_data = wp_generate_attachment_metadata($attach_id, $file[‘file’]);
wp_update_attachment_metadata($attach_id, $attach_data);
update_user_meta($user_id, $wpdb->get_blog_prefix() . ‘user_avatar’, $attach_id);
}set_avatar_url(‘https://myweb.com/Logo-test2.png’, 5);
- The topic ‘Change Avatar Programmatically’ is closed to new replies.