This is the php I have for a form which creates a post with thumbnail. I want to know how to add the uploaded image to the post content as well. Tried a few things, but they didn't work.
if(isset($_POST['new_post']) == '1') {
$post_title = $_POST['post_title'];
$post_category = $_POST['cat'];
$filename = $_POST['attach'];
$new_post = array(
'ID' => '',
'post_author' => $current_user->ID,
'post_category' => array($post_category),
'post_title' => $post_title,
'post_status' => 'draft'
);
$post_id = wp_insert_post($new_post);
$post = get_post($post_id);
$new_post = $post->ID;
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
return "upload error : " . $_FILES[$file]['error'];
}
$attach_id = media_handle_upload( $file, $new_post );
}
}
if ($attach_id > 0){
//and if you want to set that image as Post then use:
update_post_meta($new_post,'_thumbnail_id',$attach_id);
}
}