Advertisement
Guest User

Untitled

a guest
Jan 12th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. require_once(ABSPATH . 'wp-admin/includes/image.php');
  2.  
  3. $filename = 'your file name';
  4.  
  5. $attachment = array(
  6. 'post_mime_type' => 'your mime type',
  7. 'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
  8. 'post_content' => '',
  9. 'post_status' => 'inherit',
  10. 'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
  11. );
  12. $attach_id = wp_insert_attachment( $attachment, $filename,'your_post_id' );
  13. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  14. wp_update_attachment_metadata( $attach_id, $attach_data );
  15. update_post_meta($id, '_thumbnail_id', $attach_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement