i have a form that allows input of name ,email & photo...when i click on submit i want it to insert a row into the posttype called'contact' with the info submitted by the form and the image as the featured image but it does not set the image as featured image
my code
<?php
if (isset($_POST['submit'])) {
$yourname=$_POST['yourname'];
$email=$_POST['email'];
$myimage=$_POST['myimage'];
$my_post = array(
'post_title' => $youname,
'post_status' => 'publish',
'post_author' => 1,
'post-thumbnails' => $myimage
);
$post_id = wp_insert_post( $my_post );
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$imageid = insert_attachment($file,$pid);
}
}
set_post_thumbnail($post_id,$imageid);
update_field( "field_5507c9", $email, $post_id );
}
?>
<form action="" method="POST">
Your Name: <input type="text" name="yourname" value=""> <br>
Your Email: <input type="text" name="email" value=""> <br>
Image: <input type="file" name="myimage" id=""><br>
<input type="submit" name="submit" value="submit" />
</form>
add this below to functions.php
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;
}
plus is there any way i can check if row is uploaded & echo a message if insert is done and then redirect to a new url after few seconds.
insert_attachemnt
is not a WordPress function. It iswp_insert_attachemnt
. Read the documentation and try to usewp_insert_attachemnt
(read carefully the parameters this function needs, they are not the same you are currently using).