admin管理员组文章数量:1427321
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
1 Answer
Reset to default 1To 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()
版权声明:本文标题:custom post types - how to get URL of media uploaded to WordPress via media_handle_sideload() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745493447a2660704.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论