最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

uploads - WooCommerce Images are uploading to the server but not attaching

programmeradmin3浏览0评论

I've Created a Script that Grabs an image from an external site and it is imported to the WordPress image gallery. -> wp-admin/upload.php [Media Library]

My Code Snipplet

foreach($url as $linkurl) {
    $tmp = download_url( $linkurl);

    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $linkurl, $matches);

$clean = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));

$file_array = array(
    'name' => basename( $clean ),
    'tmp_name' => $tmp
);

if ( is_wp_error( $tmp ) ) {
    @unlink( $file_array[ 'tmp_name' ] );
    return $tmp;
}

$id = media_handle_sideload( $file_array, $post_id, 'gallery desc' );


$value = wp_get_attachment_url( $id );

update_post_meta($post_id,  array_push($post_id, '_product_image_gallery', $id));

//update_post_meta($post_id,  array_push($post_id, '_product_image_gallery', $value ));

}

What am I doing Wrong

I've Created a Script that Grabs an image from an external site and it is imported to the WordPress image gallery. -> wp-admin/upload.php [Media Library]

My Code Snipplet

foreach($url as $linkurl) {
    $tmp = download_url( $linkurl);

    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $linkurl, $matches);

$clean = str_replace(array("%2B", "%52", "%20", "%5"), "B", basename($matches[0]));

$file_array = array(
    'name' => basename( $clean ),
    'tmp_name' => $tmp
);

if ( is_wp_error( $tmp ) ) {
    @unlink( $file_array[ 'tmp_name' ] );
    return $tmp;
}

$id = media_handle_sideload( $file_array, $post_id, 'gallery desc' );


$value = wp_get_attachment_url( $id );

update_post_meta($post_id,  array_push($post_id, '_product_image_gallery', $id));

//update_post_meta($post_id,  array_push($post_id, '_product_image_gallery', $value ));

}

What am I doing Wrong

Share Improve this question edited Nov 15, 2015 at 18:15 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Nov 15, 2015 at 17:42 Chen ChiuChen Chiu 113 bronze badges 1
  • 1 check my answer here with the solution stackoverflow/questions/46615976/… – JordanBel Commented Oct 7, 2017 at 16:43
Add a comment  | 

1 Answer 1

Reset to default 1

I think that you have made a mistake in the last line. array_push is not doing what you are trying to achieve (check the Manual) same as update_post_meta accepts different arguments.

Besides that WooCommerce is storing galleries in simple comma separated list like 1,45,78,34 so you should change the last line
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $id));
to
update_post_meta($post_id, '_product_image_gallery', $id);
or if you add multiple images then instead of $id enter comma separated list.

发布评论

评论列表(0)

  1. 暂无评论