Last active
August 29, 2015 14:13
-
-
Save sunnyluthra/34f180271aca9b794bf8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
$uploadedfile = $_FILES['file']; | |
$upload_overrides = array( 'test_form' => false ); | |
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides ); | |
if ( $movefile ) { | |
$wp_filetype = $movefile['type']; | |
$filename = $movefile['file']; | |
$wp_upload_dir = wp_upload_dir(); | |
$attachment = array( | |
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ), | |
'post_mime_type' => $wp_filetype, | |
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), | |
'post_content' => '', | |
'post_status' => 'inherit' | |
); | |
//$post_id (the post you want this image to be attached with) | |
$attach_id = wp_insert_attachment( $attachment, $filename, $post_id); | |
// Generate the metadata for the attachment, and update the database record. | |
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); | |
wp_update_attachment_metadata( $attach_id, $attach_data ); | |
} else { | |
echo "Possible file upload attack!\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment