// $filename should be the path to a file in the upload directory.
$filename = ‘/path_to_upload/filename.jpg’;

// The ID of the post this attachment is for.
$parent_post_id = $post_id;

// Check the type of file.
$filetype = wp_check_filetype( basename( $filename ), null );

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

// Prepare an array of post data for the attachment.
$attachment = array(
‘guid’ => $wp_upload_dir[‘url’] . ‘/’ . basename( $filename ),
‘post_mime_type’ => $filetype[‘type’],
‘post_title’ => preg_replace( ‘/\.[^.]+$/’, ”, basename( $filename ) ),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);

// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );

require_once( ABSPATH . ‘wp-admin/includes/image.php’ );

// Generate the metadata for the attachment
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );

set_post_thumbnail( $parent_post_id, $attach_id );

Please login or Register to submit your answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.