Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require_once(ABSPATH . 'wp-admin/includes/image.php');
- $filename = 'your file name';
- $attachment = array(
- 'post_mime_type' => 'your mime type',
- 'post_title' => preg_replace('/.[^.]+$/', '', basename($filename)),
- 'post_content' => '',
- 'post_status' => 'inherit',
- 'guid' => $wp_upload_dir['url'] . '/' . basename($filename)
- );
- $attach_id = wp_insert_attachment( $attachment, $filename,'your_post_id' );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- update_post_meta($id, '_thumbnail_id', $attach_id);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement