wordpress create post with php

Arun - 09/02/2020 8:33 AM

wordpress create post with php

Files 1

header.php
php
<?php
	
	
	require ( ABSPATH . 'wp-admin/includes/image.php' );
	require_once(ABSPATH . 'wp-admin/includes/file.php');
	require_once(ABSPATH . 'wp-admin/includes/media.php');
	$uploaddir = wp_upload_dir();
	$post_id = wp_insert_post( array(
    'post_title' => 'Customized ' . $_POST['form_material']." Watch",
    'post_content' => '',
    'post_status' => 'publish',
    'post_type' => "product",
) );
	wp_set_object_terms( $post_id, 'simple', 'product_type' );
	wp_set_object_terms( $post_id, array(11), 'product_cat' );
	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' ); 
if(strcmp($_POST['form_material'],'Silicone')==0)
{
	$price=89;
}
else
{
	$price=399;
}
update_post_meta( $post_id, '_regular_price', $price);
update_post_meta( $post_id, '_sale_price', '' );
update_post_meta( $post_id, '_purchase_note', '' );
update_post_meta( $post_id, '_featured', 'no' );
update_post_meta( $post_id, '_weight', '' );
update_post_meta( $post_id, '_length', '' );
update_post_meta( $post_id, '_width', '' );
update_post_meta( $post_id, '_height', '' );
update_post_meta( $post_id, '_sku', '' );
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, 'watchtype', $_POST['form_material'] );
update_post_meta( $post_id, 'zoomin', $_POST['form_scale'] );
update_post_meta( $post_id, 'rotate', $_POST['form_rotate_angle'] );
update_post_meta( $post_id, 'left_offset', $_POST['form_crop_left'] );
update_post_meta( $post_id, 'top_offset', $_POST['form_crop_top'] );
update_post_meta( $post_id, 'pattern_name', $_POST['form_pattern_name'] );
 
 
 
$session_id = substr($session_id,0,8);
global $wpdb;
$get_generated_image = $wpdb->get_results( "SELECT image_url, image_path FROM {$wpdb->prefix}imagine WHERE session_id='{$session_id}' AND image_status = 'preview'", 'ARRAY_A' );
$generated_single_image_url = $get_generated_image[0]['image_url']; //array of images 0,1 for images
$ext = pathinfo($generated_single_image_url, PATHINFO_EXTENSION);
 
$filename = $uploaddir['path'] . '/' . 'Design'.$post_id.".".$ext;
$filenameCsv = $uploaddir['path'] . '/' . 'file'.$post_id.".csv";+
$print_file = '';
 
$contents = file_get_contents($generated_single_image_url);
$savefile = fopen($filename, 'w');
fwrite($savefile,$contents);
fclose($savefile);
$wp_filetype = wp_check_filetype(basename($filename), null );
 
$attachment = array(
	'guid'           => $uploaddir['url'] . '/' . basename( $filename ), 
    'post_mime_type' => $wp_filetype['type'],
 
    'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
 
    'post_content' => '',
 
    'post_status' => 'inherit'
 
);
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
 
$imagenew = get_post( $attach_id );
 
$fullsizepath = get_attached_file( $imagenew->ID );
 
$attach_data = wp_generate_attachment_metadata( $attach_id, $fullsizepath );
wp_update_attachment_metadata( $attach_id, $attach_data );
 
WC()->instance()->cart->add_to_cart($post_id);
set_post_thumbnail($post_id,$attach_id);
 
 
 
 
$attachments = array($filenameCsv,$filename,$print_file);
   $headers = 'From: Ideyah.com <[email protected]>' . "\r\n";
   $msg='Hello Admin, New Order Placed Please check';
   $admin_email = get_option( 'admin_email' );
   $admin_email = '[email protected]';
   $mail_subject = "New Order Placed - " . rand(12010,34567);
   wp_mail($admin_email, $mail_subject,  $msg, $headers, $attachments);
   
   die;