Viewing 1 replies (of 1 total)
  • Emulator000

    (@emulator000)

    Hi,
    for set an image to the user avatar simply do this:
    delete_metadata(‘post’, null, ‘_wp_attachment_wp_user_avatar’, $user_id, true);

    update_user_meta($user_id, ‘_wp_attachment_wp_user_avatar’, $attach_id);

    update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . ‘user_avatar’, $attach_id);

    For create an attachment:
    $upload_dir = wp_upload_dir();

    $wp_filetype = wp_check_filetype(basename($file), null);

    $attachment = array(
    ‘guid’ => $upload_dir[‘url’] . ‘/’ . basename($file),
    ‘post_mime_type’ => $wp_filetype[‘type’],
    ‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename($file)),
    ‘post_content’ => ”,
    ‘post_status’ => ‘inherit’
    );

    $attach_id = wp_insert_attachment($attachment, $file);
    $attach_data = wp_generate_attachment_metadata($attach_id, $file);

    wp_update_attachment_metadata($attach_id, $attach_data);

Viewing 1 replies (of 1 total)

The topic ‘Setting user's avatar programmatically?’ is closed to new replies.