Reputation: 87
I'm trying to change product image via php. But I can't get it to work.
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$yesterday = date("Y-m-d", time() - 60 * 60 * 24);
$jsondata = file_get_contents("730.json");
ini_set('memory_limit', '-1');
$json = json_decode($jsondata, true);
$price = $json["AWP | Asiimov (Well-Worn)"][$yesterday]["price"]/100;
$post_information = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "publish",
'post_title' => 'test',
'post_parent' => '',
'post_type' => "product",
);
$image = media_sideload_image("http://www.technologijos.lt/upload/image/n/technologijos/it/S-49657/samsung-ssd-840-evo.jpg", 0, "The WordPress Logo");
$post_id = wp_insert_post($post_information);
$attachment = wp_get_attachment_image( 18, 'thumbnail' );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'no');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', $price );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_featured', "no" );
update_post_meta($post_id, '_sku', "145");
update_post_meta( $post_id, '_product_attributes', array());
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', $price );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
update_post_meta( $post_id, '_backorders', "no" );
update_post_meta( $post_id, '_stock', "" );
update_post_meta( $post_id, '_product_image_gallery', $attachment );
The last line doesnt work. And i dont know how to get it working. I tried changing different things, (I am new to PHP/ WordPress).
Upvotes: 2
Views: 8252
Reputation: 267
I would like to add to bryceadams answer.
Seems like that, for example, eBay's Finding API returns images whose URL has a structure that will not allow the command
$filename = basename($image_url);
to operate as expected because, what happens is, all URLs of the images have the same ending basename and the files get conflicted. I found out that the command
$filename = basename($image_url, '.jpg') . (string) rand(0, 5000) . '.jpg';
works better for many APIs. Hope it adds up and helps.
Upvotes: 0
Reputation: 2352
Are you trying to set the featured image?
If so, you may want to use the function given in the answer here.
The Function:
function Generate_Featured_Image( $image_url, $post_id ){
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if(wp_mkdir_p($upload_dir['path'])) $file = $upload_dir['path'] . '/' . $filename;
else $file = $upload_dir['basedir'] . '/' . $filename;
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
$res1= wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $post_id, $attach_id );
}
Usage:
Generate_Featured_Image( '../wp-content/my_image.jpg', $post_id );
// $post_id is Numeric ID... You can also get the ID with: wp_insert_post()
Upvotes: 4