1

I'm trying to upload a photo from an URL then set it as the feature image of a product that user is submitting

Now I can do the first part of this function the problem is I can't get the new URL of the media and set it as the feature image for WooCommerce product

here is the code

add_action('transition_post_status', 'new_product_add', 10, 3);
function new_product_add($new_status, $old_status, $post) {
    if( 
        $old_status != 'publish' 
        && $new_status == 'pending' 
        && !empty($post->ID) 
        && in_array( $post->post_type, 
            array( 'product') 
            )
        ) {
            $external_url = $post->post_excerpt;
            /********************************************/
            function uploadImageToMediaLibrary($postID, $url, $alt = "blabla") {
                $tmp = download_url( $url );
                $desc = $alt;
                $file_array = array();
                preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $url, $matches);
                $file_array['name'] = basename($matches[0]);
                $file_array['tmp_name'] = $tmp;
                if ( is_wp_error( $tmp ) ) {
                    @unlink($file_array['tmp_name']);
                    $file_array['tmp_name'] = '';
                }
                $id = media_handle_sideload( $file_array, $postID, $desc);
                if ( is_wp_error($id) ) {
                    @unlink($file_array['tmp_name']);
                    return $id;
                }
                return $id;
            }
            uploadImageToMediaLibrary($post->ID, $external_url, "custom_alt");

                //MEDIA UPLOADED SUCCESSFULLY
                //I DONT KNOW HOW TO GET THE NEW MEDIA URL FROM MEDIA GALLERY 
                //AND SET IT FOR THE PRODUCT FEATURE IMAGE


                /********************************************/
                $term = get_term_by('name', 'PARENT_CATEGORY', 'product_cat');
                wp_set_object_terms($post->ID, $term->term_id, 'product_cat', true);
        }
    }
1

To get the URL of the uploaded image/attachment, you can use wp_get_attachment_url() (which always returns the full-sized image URL) or wp_get_attachment_image_url() for image attachment:

// This is how you should call uploadImageToMediaLibrary(); assign the value to $att_id.
$att_id = uploadImageToMediaLibrary($post->ID, $external_url, "custom_alt");
$url = wp_get_attachment_image_url( $att_id, 'full' ); // full-sized URL

But to set the image as the featured image of the post where the image was uploaded to, or any posts actually, then you would use set_post_thumbnail() like so:

$att_id = uploadImageToMediaLibrary($post->ID, $external_url, "custom_alt");
if ( ! is_wp_error( $att_id ) && $att_id ) {
  set_post_thumbnail( $post->ID, $att_id );
}

PS: I revised this answer because using set_post_thumbnail() is much preferred than manually updating the private metadata _thumbnail_id used for post featured images.. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.