Own plugin: how can I attach files to a post using wp_insert_attachment
-
I’ve created few years ago my own forum, where are attached lot of files.
I decided to stop using the whole forum, and to migrate everything into WordPress. My problem are attachments.
Let say I have created my $postid with
// Create post $postid = $this->insertPost($wpdb->escape($myboard->title), $wpdb->escape($myboard->$content), $myboard->$date, $myboard->$categories, $myboard->posttype, $myboard->authorid, $myboard->allowpings, $myboard->comment_status, $meta);
Let say I made a copy of an image attached to a post, here /wp-content/plugin/myboard/cache_files/
My question, is how can I attached my files (a thumbnail) to my post ?
I’ve been trying this :
if(file_exists($thumbnail)) { $file = wp_load_image($thumbnail); if (!is_resource($file)) { echo "Noway, this files doens't exists"; die(); } $attachment = array( 'post_title' => 'file'. time(), 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $postid, 'post_mime_type' => 'image/jpeg' ); // Save the data $id = wp_insert_attachment($attachment, $file, $postid ); wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); }
With the code above, some media has been added in “Medialibrary” but I can’t see any of the files (Media Library says “Missing Attachment”) and of course I’ve no idea how copy the files in the right upload folders (instead of letting my files in /wp-content/plugin/myboard/cache_files/ I would like them to be in /wp-content/upload/2008/06/ for exemple)
Any advices would really be appreciate
- The topic ‘Own plugin: how can I attach files to a post using wp_insert_attachment’ is closed to new replies.