$my_post = array(
‘post_title’ => ”,
‘post_content’ => ”,
‘post_status’ => ‘publish’,
‘post_type’ => ‘media_center’, // we can set custom post type too
);
$postid = wp_insert_post( $my_post );
$uploadedfile = $_FILES[‘image_file’];
$upload_overrides = array( ‘test_form’ => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if( $movefile ) {
$file = $movefile[‘file’];
$filename = $_FILES[‘image_file’][‘name’];
$url = $movefile[‘url’];
//echo”igex”.$url;
$filetype = wp_check_filetype( basename( $file ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
‘guid’ => $wp_upload_dir[‘url’] . ‘/’ . basename( $file ),
‘post_mime_type’ => $filetype[‘type’],
‘post_title’ => preg_replace( ‘/\.[^.]+$/’, ”, basename( $file ) ),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);
$attach_id = wp_insert_attachment( $attachment, $file, $posts );
require_once( ABSPATH . ‘wp-admin/includes/image.php’ );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
$c = set_post_thumbnail( $postid, $attach_id );
}