Skip to content

Instantly share code, notes, and snippets.

@DWboutin
Last active August 29, 2015 14:00
Show Gist options
  • Save DWboutin/11379765 to your computer and use it in GitHub Desktop.
Save DWboutin/11379765 to your computer and use it in GitHub Desktop.
Wordpress custom upload with thumbnail creation
<?php
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
if(isset($_FILES['photo']['name']) && !empty($_FILES['photo']['name'])){
//Upload la photo dans le dossier
$uploadedfile = $_FILES['photo'];
$movefile = wp_handle_upload($uploadedfile, array('test_form' => false));
//On sauvegarde la photo dans le média library
if ($movefile) {
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'].'/'.basename($movefile['file']),
'post_mime_type' => $movefile['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($movefile['file'])),
'post_content' => '',
'post_status' => 'inherit'
);
// on crée les informations et génère les thumnails
$attach_id = wp_insert_attachment($attachment, $movefile['file']);
$attach_data = wp_generate_attachment_metadata( $attach_id, $movefile['file'] );
$update = wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment