Last active
August 29, 2015 14:00
-
-
Save DWboutin/11379765 to your computer and use it in GitHub Desktop.
Wordpress custom upload with thumbnail creation
This file contains hidden or 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
<?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