Last active
August 30, 2019 21:03
-
-
Save eto4detak/4bb96923bdc3f81cb4eac474bafe03e2 to your computer and use it in GitHub Desktop.
wp php dowload file
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/image.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
require_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
$file_array = []; | |
$tmp = download_url($this->infoProduct[0]['image_src'][0]); | |
preg_match('/[^\?]+\.(jpg|jpe|jpeg|gif|png)/i', $this->infoProduct[0]['image_src'][0], $matches ); | |
$file_array['name'] = basename($matches[0]); | |
$file_array['tmp_name'] = $tmp; | |
$media_id = media_handle_sideload( $file_array, $post_id); | |
if( is_wp_error($media_id) ) { | |
@unlink($file_array['tmp_name']); | |
echo $media_id->get_error_messages(); | |
} | |
@unlink( $file_array['tmp_name'] ); | |
set_post_thumbnail($post_id, $media_id); | |
if (!empty( $_FILES['files-image'] )) { | |
$files = $_FILES['files-image']; | |
foreach ($files['name'] as $key => $value) { | |
if ($files['name'][$key]) { | |
$file = array( | |
'name' => $files['name'][$key], | |
'type' => $files['type'][$key], | |
'tmp_name' => $files['tmp_name'][$key], | |
'error' => $files['error'][$key], | |
'size' => $files['size'][$key] | |
); | |
$gallery_attachments[] = media_handle_sideload($file, $post_id); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment