I have a form which updates a post but when updating I need to be able to also upload 3 posts:
<div class="form-group">
<input class="form-control" type="file" tabindex="3" name="custom-upload1" id="custom-upload2" />
<input class="form-control" type="file" tabindex="3" name="custom-upload2" id="custom-upload2" />
<input class="form-control" type="file" tabindex="3" name="custom-upload3" id="custom-upload3" />
</div>
The post I am updtaing is a draft which I then make it publish and within the same form I need to be able to also change the title, so within the same template I also have:
<div class="form-group">
<input id="theTitle" class="form-control" type="hidden" name="inputTitle" value="<?php echo $myTitle; ?>" autocomplete="nope">
</div>
<?php if ($post->post_author == $current_user->ID) {
toPubblish($post->ID); ?>
<input type="hidden" value="ok" name="pubblish">
<?php
if ( get_post_status ($post->ID) == 'publish' ) {
} else { ?>
<input id="upTitle" type="submit" name="newTitle" value="BUONA SERATA" class="btn secondary-btn primary-bg">
<?php }
} ?>
And then I do
if($_SERVER['REQUEST_METHOD']=="POST") {
if ('BUONA SERATA' === ($_POST['newTitle'])) {
$post = array(
'ID' => $id,
'post_title' => $_POST['inputTitle']
);
wp_update_post($post, true);
if ($_FILES['postImage']) {
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
//Add your error action
} else {
$attach_id = media_handle_upload( $file, $post->ID );
}
}
}
if ($attach_id > 0){
update_post_meta($post,'bgc_event_pimg',$attach_id);
}
ob_start(); //this should be first line of your page
$myurl = get_site_url();
header('Location: '.$myurl."/profile/");
ob_end_flush(); //this should be last line of your page
}
}
And in function I have:
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
But the images are uploading but are not attaching to current posts and I cannot see them if I see the posts