I am working in that too, BUT…
When I add in functions.php this code
<?php
function insert_attachment($file, $post_id, $setthumb = 'false') {
// check to make sure its a successful upload
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK)
__return_false();
$post_id = $_POST['post_id'];
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, $post_id);
if ($setthumb)
update_post_meta($post_id, '_thumbnail_id', $attach_id);
return $attach_id;
}
?>
And in the home.php after wp_insert_post part I add
$wp_filetype = wp_check_filetype(basename($file), null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($file)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
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);
Now I get a file uploaded in the media admin page and attached to it’s custom-post.
But the file is not an image and in the front page Comes ” Missing Attachment “
I get tired and mad after ten days work in that, Can any body help please?