• Resolved Toucouleur

    (@toucouleur)


    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

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Toucouleur

    (@toucouleur)

    found myself the solution, but I’m really desapointed about the community here. Nor an advice or a question, I felt a bit alone ??

    Good morning. Most of us were sleeping.

    Next time, give it a bit more time…

    Care to share your solution?

    but I’m really desapointed about the community here. Nor an advice or a question, I felt a bit alone ??

    You are from France? Try the french version then?

    Could you share your solution, btw?

    I was trying to do something similar to Toucouleur and I’ve updated the function reference for wp_insert_attachment() with what I learnt.

    Based on his code I think Toucouleur’s problem was that he needed to pass the location of the filename as a string to the wp_insert_attachment() function and not a file resource object. Or at least that’s how I’m doing it ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Own plugin: how can I attach files to a post using wp_insert_attachment’ is closed to new replies.