Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. <?php
  2. $file = '/path/to/file.png';
  3. $filename = basename($file);
  4. $upload_file = wp_upload_bits($filename, null, file_get_contents($file));
  5. if (!$upload_file['error']) {
  6. $wp_filetype = wp_check_filetype($filename, null );
  7. $attachment = array(
  8. 'post_mime_type' => $wp_filetype['type'],
  9. 'post_parent' => $parent_post_id,
  10. 'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
  11. 'post_content' => '',
  12. 'post_status' => 'inherit'
  13. );
  14. $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $parent_post_id );
  15. if (!is_wp_error($attachment_id)) {
  16. require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  17. $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
  18. wp_update_attachment_metadata( $attachment_id, $attachment_data );
  19. }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement