Just need some advice on uploading images.
I am using ACF / oEmbed to display a video in my blog and in the process extracts the thumbnail from Wistia which works fine. However, I need to put the extracted thumbnail as a featured image
to the media library so it will also be accessible by the user. The code below extracts the image everytime the page gets updated.
Note: I haven't written the part that will detect if the image already exists because part of it is the problem I am having.
add_action('acf/save_post', 'my_acf_save_post');
function my_acf_save_post( $post_id ) {
$posttype = get_post_type($post_id);
$wistia_video_url = get_field('wistia', false, false);
if ('post' == $posttype && !empty($wistia_video_url)) {
$explode_url = explode('/', $wistia_video_url);
$wistia_img = extractThumbnail($explode_url[count($explode_url) - 1]);
#the problem is here
$image = media_sideload_image( $wistia_img, $post_id, ' ' ); //Put it in an attachment of the same month as the post id
set_post_thumbnail($post_id, $image);
}
}
The problem I am having is that media_sideload_image
puts it in the year and month of when the blog was published. For example, 2022/06/<file_name>.jpg
$upload_dir = wp_upload_dir();
echo $upload_dir['path'] . '<br />';
echo $upload_dir['baseurl'] . '<br />';
The code above all returns the today's month.
What would be the best way to make sure that I am always getting the correct file paths with the dates. I am fine if the image gets put in September or June as long as I am always getting it correctly?