SHARE
TWEET

Untitled

a guest Jan 12th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
Top