Hi,
I have an import plugin with an image filed in it. This field contains the relative filename which is already manually uploaded on the server (by the user) in, let's say, the /wp-content/import/images/ directory.
The goal is to pick up this file and move it to the /wp-content/uploads/ folder with the right date path in it. How can I do this automatically?
I have tried two ways, but both don't work.
Way 1:
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
This works, but the image stays in the /import/images/ directory, which is not the solution since I want them in a /uploads/2011/11/ structure.
Way 2:
Use the media_handle_upload() function:
$attach_id = media_handle_upload( $file_handler, $post_id );
This seems to be a solution, but the problem with this one is that the media_handle_upload function uses the index of the $_FILES variable and since, I'm not directly uploading pictures, this is not a working solution.
Anyone a solution or ideas for this?
Thanks,
Arjan