Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zarankumar/dfed301cb5a62c448129d70624af7bc1 to your computer and use it in GitHub Desktop.
Save zarankumar/dfed301cb5a62c448129d70624af7bc1 to your computer and use it in GitHub Desktop.
<?php
// Need to require these files
if ( !function_exists('media_handle_upload') ) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
// All data
$boat_title=$_POST['boattitle'];
$post = array(
'post_author' => $user_id,
'post_content' => '',
'post_status' => "pending",
'post_title' => $boat_title,
'post_parent' => '',
'post_type' => "product",
);
//Create post
$post_id = wp_insert_post( $post);
// set Featured Image , Let WordPress handle the upload.
$attachment_id = media_handle_upload( 'my_boat_image_upload', $post_id );
if ( is_wp_error( $attachment_id ) ) {
// There was an error uploading the image.
wp_send_json(array('status'=>false,'msg'=>"upload error"));
} else {
// The image was uploaded successfully!
add_post_meta($post_id, '_thumbnail_id',$attachment_id);
}
///. Gallery Images....//
$list_id="";
$files = $_FILES["my_gallery"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array ("my_gallery" => $file);
foreach ($_FILES as $file => $array) {
$attach_id= media_handle_upload( $file, $post_id);
$list_id=$attach_id.",".$list_id;
}
}
}
update_post_meta($post_id,'_product_image_gallery',$list_id);
//if($post_id){
// $attach_id = get_post_meta($product->parent_id, "_thumbnail_id", true);
// add_post_meta($post_id, '_thumbnail_id', $attach_id);
//}
wp_set_object_terms($post_id, 'Races', 'product_cat' );
wp_set_object_terms($post_id, 'simple', 'product_type');
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, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', "1" );
update_post_meta( $post_id, '_sale_price', "1" );
update_post_meta( $post_id, '_purchase_note', "" );
update_post_meta( $post_id, '_sale_price_dates_from', "" );
update_post_meta( $post_id, '_sale_price_dates_to', "" );
update_post_meta( $post_id, '_price', "1" );
update_post_meta( $post_id, '_sold_individually', "" );
update_post_meta( $post_id, '_manage_stock', "no" );
//wp_send_json($post_id);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment