Title: codegrunt's Replies | WordPress.org

---

# codegrunt

  [  ](https://wordpress.org/support/users/codegrunt/)

 *   [Profile](https://wordpress.org/support/users/codegrunt/)
 *   [Topics Started](https://wordpress.org/support/users/codegrunt/topics/)
 *   [Replies Created](https://wordpress.org/support/users/codegrunt/replies/)
 *   [Reviews Written](https://wordpress.org/support/users/codegrunt/reviews/)
 *   [Topics Replied To](https://wordpress.org/support/users/codegrunt/replied-to/)
 *   [Engagements](https://wordpress.org/support/users/codegrunt/engagements/)
 *   [Favorites](https://wordpress.org/support/users/codegrunt/favorites/)

 Search replies:

## Forum Replies Created

Viewing 1 replies (of 1 total)

 *   Forum: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
   
   In reply to: [function for attaching existing attachment to a post](https://wordpress.org/support/topic/function-for-attaching-existing-attachment-to-a-post/)
 *  [codegrunt](https://wordpress.org/support/users/codegrunt/)
 * (@codegrunt)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/function-for-attaching-existing-attachment-to-a-post/#post-2835540)
 * I am very new to WordPress but I expect you can find what you want by following
   through what media_handle_upload() does in “wp-admin/includes/media.php”.
 * Ultimately, you have to do the following to create the attachment:
 *     ```
       // Save the data
       	$id = wp_insert_attachment($attachment, $file, $post_id);
       	if ( !is_wp_error($id) ) {
       		wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
       	}
       ```
   
 * Setting up the attachment array looks relatively straight forward since it is
   pulling mostly out the $_POST array which in your case would be replaced with
   the known values for the preset file you want to attach.
 * Cheers

Viewing 1 replies (of 1 total)