I'm trying to programmatically upload images to the media library by taking an ACF url, running it through the Wordpress function and then updating a taxonomy field.
I know for a fact that $icon
is the correct URL and the update_field
function is correct.
The part that is going wrong is the upload image part. I think I've followed the documentation correctly but no image is uploaded into the library and nothing is connected to the ACF field on the taxonomy.
Where am I going wrong?
// Icon
$icon = get_field('icon', $term->taxonomy . '_' . $term->term_id);
$field_key = "field_5ad5e7e5e295c";
if($icon != "") {
// The ID of the post this attachment is for.
$return = "id";
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
// Generate the metadata for the attachment, and update the database record.
$attach_id = media_sideload_image($icon, $post_id, $desc, id);
update_field( $field_key, $attach_id, $taxonomy.'_'.$french );
update_field( $field_key, $attach_id, $taxonomy.'_'.$german );
update_field( $field_key, $attach_id, $taxonomy.'_'.$italian );
update_field( $field_key, $attach_id, $taxonomy.'_'.$russian );
update_field( $field_key, $attach_id, $taxonomy.'_'.$spanish );
}