admin管理员组

文章数量:1022853

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);
        }
    }

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);
        }
    }
Share Improve this question edited May 8, 2019 at 17:38 zEn feeLo asked May 8, 2019 at 17:15 zEn feeLozEn feeLo 2073 silver badges18 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.. =)

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);
        }
    }

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);
        }
    }
Share Improve this question edited May 8, 2019 at 17:38 zEn feeLo asked May 8, 2019 at 17:15 zEn feeLozEn feeLo 2073 silver badges18 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 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.. =)

本文标签: custom post typeshow to get URL of media uploaded to WordPress via mediahandlesideload()