2

I've got a plugin that transfers some files over to the uploads folder of the site in which the plugin is installed.

It works fine, however, the images are not appearing in the Media Manager. I expect some database registration is involved.

Given the script below which copies the files into the directory, what command would I need to add to the loop to register each image for the media manager?

    foreach(new RecursiveIteratorIterator($rdi) as $files) 
    {
         if ($files->isFile()) 
         {
            $imagepath = $files->getRealPath();
            $image = basename($files->getPathname());
            copy($imagepath, $my_target_folder.'/'.$image);
         }
    }

2 Answers 2

3

add this to your for each and $filename to each file,

 $wp_filetype = wp_check_filetype(basename($filename), null );
  $attachment = array(
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
     'post_content' => '',
     'post_status' => 'inherit'
  );
  $attach_id = wp_insert_attachment( $attachment, $filename, 0 );
  // 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, $filename );
  wp_update_attachment_metadata( $attach_id,  $attach_data );
4
  • @Scott: let me know how it goes
    – Bainternet
    Commented Mar 23, 2011 at 22:33
  • what value should I use for $filename? eg, would I use my $image variable or my $imagepath variable? ($image = test.png, $imagepath = "c:/host/sites/...")
    – Scott B
    Commented Mar 23, 2011 at 22:35
  • @Scott: $filename - Location of the file on the server. Use absolute path and not the URI of the file. The file MUST be on the uploads directory.
    – Bainternet
    Commented Mar 23, 2011 at 22:39
  • Got it. +1 for the excellent help. I really appreciate it.
    – Scott B
    Commented Mar 23, 2011 at 22:46
6

Have you looked at the function media_handle_sideload()? It seems to be what you're looking for.

It works essentially the same way as media_handle_upload(), but takes a file thats already on the local server, moves it to the current uploads directory, and generates the post record necessary to add it to the Media Manager.

5
  • @GA: little nuggets like this is why i come to SA. I'd already selected the answer and you still thought to offer a second option. I sincerely love this place! +1
    – Scott B
    Commented Mar 24, 2011 at 2:19
  • @Scott B thanks - yeah, I try to answer anything that I know, whether an answer has been accepted or not. Someone else will always find this question searching for something, and its good to have these answers archived! Commented Mar 24, 2011 at 5:53
  • @GA: There's not much info on this function. I did find a use-case example here > wordpress.org/support/topic/… - There appear to be some issues prior to a 3.1 related bug fix. I'm holding out hope of adopting this in replacement of @Bainternet's suggestion as it seems more appropriate.
    – Scott B
    Commented Mar 24, 2011 at 17:20
  • Here's a link to the bug track issue > core.trac.wordpress.org/ticket/15432
    – Scott B
    Commented Mar 24, 2011 at 17:22
  • Interesting. It looks like that patch has been applied since 3.01, though... Commented Mar 24, 2011 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.