1

What code currently does:

I feed post id & previously uploaded image id, code processes it all and does its magic described below. Result: unattached old image and new attached image.


What I need it to do:

Attach that same image to post as you can do in media screen with few clicks.


Q: Does anyone know a fix? Or maybe I made a mistakes somewhere? All comments are welcomed!


//Attach images to post    
$gallery_string = $_POST['my-gallery'];

if( !empty( $gallery_string ) ) {

    //It's a string (e.g '1424,3435,7544,7332') - turn it into array
    $gallery_array = explode(',', $gallery_string);

    foreach( $gallery_array as $value ) {

        //Full path of attachment
        $filename = get_attached_file( $value );

        //Path to the upload directory
        $wp_upload_dir = wp_upload_dir();

        //Type of file
        $filetype = wp_check_filetype( basename( $filename ), null );

        //Prepare an array of post data for the attachment
        $attachment = array(
          'guid'           => $wp_upload_dir['url'] . '/' . basename( $filename ),
          'post_mime_type' => $filetype['type'],
          'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
          'post_content'   => '',
          'post_status'    => 'inherit'
        );

        //Insert the attachment
        $attach_id = wp_insert_attachment( $attachment, $filename, $target_post_id );

        //Needed for wp_generate_attachment_metadata()
        require_once( ABSPATH . 'wp-admin/includes/image.php' );

        //Generate the metadata for the attachment and update the database record
        $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
        wp_update_attachment_metadata( $attach_id, $attach_data );

    }
}

I know that I could use the same image ID and wp_delete_attachment() to delete it after "duplication" but still.. Why to make things more complicated if (IF) there's a way to just attach it?

I also looked into upload.php in wp-admin folder for the action that gets triggered when you press "attach" button in media page but it seems that it can't be used in our code (if it does, it requires pretty big hassle).

  • Could you elaborate a bit why are you doing it this way? On top of my head attachment is just a post parent designation, this looks like you are doing a complete re-import of existing file or something. – Rarst Dec 1 '15 at 21:00
  • I'll add some background information to my question as we speak, If you know how to create parent relation / attach already uploaded file to post, be so kind and let me know. – N00b Dec 1 '15 at 21:06
0

Attachment is just post_parent value of the attachment post object, you can update that via wp_update_post:

foreach( $gallery_array as $value ) {
    $the_post = array(
        'ID'            => $value,
        'post_parent'   => $target_post_id,
    );
    wp_update_post( $the_post );
}
  • How is this even possible?! There are so many question a'la "how to attach file to post with code" but zero correct answers! Thanks Milo! Super simple and works like a charm. – N00b Dec 1 '15 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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