How to add attachment without uploading?

When save post, I made dir in wp_uploads_dir, and copied 2 files and one folder in this dir. Now I want to do the WP_insert_attatchment process to make the 2 files as attachements. How can I do it?

Edit: I inset_attatchement by directly using their URL. Now they show up in Media Libray, but I can’t see whether the attachment_id is recorded or not. Which table in database can I go to find it?

1 Answer
1

You need the local path to add an attachment:

// add the file to the media library
$attachment = array(
    'post_mime_type' => 'image/png' // the MIME type
,   'post_title'     => 'Attachment title'
);

// Adds the file to the media library and generates the thumbnails.
$attach_id = wp_insert_attachment( $attachment, $path ); // PATH!

Leave a Comment