I have ACF fields added to a custom block for Gutenberg. The group of fields "youtube_videos" contains a repeater "videos" with two subfields "video" (url) and "thumb" (picture). I add links to the "video" and display them on the site. I would like to save previews from the video automatically when the video pages are called, i.e. if there is no picture in the "thumb", then pick it up from youtube and save it in the field. There are no problems with downloading images and saving them to the Wordpress media library, it works. But I can't update the fields in any way. I will be grateful for any hint. That's the kind of construction I get function get_youtube_id($url) { $regex = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?\/|.*[?&]v=)|youtu\.be\/))([^"&?\/\s]{11})/i'; if (preg_match($regex, $url, $matches)) { return $matches[1]; } return null; } require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $updated_videos = []; $field = get_field('youtube_videos'); if (!empty($field['videos'])) { foreach ($field['videos'] as $item) { $video_url = $item['video']; $youtube_id = get_youtube_id($video_url); if (empty($item['thumb']['ID'])) { $thumbnail_url = "https://img.youtube.com/vi/$youtube_id/hqdefault.jpg"; $thumb_id = media_sideload_image($thumbnail_url, 0, null, 'id'); if (!is_wp_error($thumb_id)) { $thumb_data = get_post($thumb_id); $item['thumb'] = [ 'ID' => $thumb_id, 'title' => $thumb_data->post_title, 'filename' => 'youtube_image_' . $youtube_id . '_preview.jpg', 'filesize' => filesize(get_attached_file($thumb_id)), 'url' => wp_get_attachment_url($thumb_id), 'link' => get_attachment_link($thumb_id), 'alt' => get_post_meta($thumb_id, '_wp_attachment_image_alt', true), 'author' => $thumb_data->post_author, 'description' => $thumb_data->post_content, 'name' => $thumb_data->post_name, 'status' => $thumb_data->post_status, 'uploaded_to' => $thumb_data->post_parent, 'modified' => $thumb_data->post_modified, 'mime_type' => $thumb_data->post_mime_type, 'sizes' => wp_get_attachment_metadata($thumb_id)['sizes'] ?? [], ]; } } $updated_videos[] = [ 'video' => $video_url, 'thumb' => $item['thumb'], ]; } update_field('youtube_videos', ['videos' => $updated_videos], $block['id']); }