Skip to content

Instantly share code, notes, and snippets.

@nick-kravchenko
Last active July 17, 2017 15:27
Show Gist options
  • Save nick-kravchenko/7d990184f30a5a09310f39e52152b283 to your computer and use it in GitHub Desktop.
Save nick-kravchenko/7d990184f30a5a09310f39e52152b283 to your computer and use it in GitHub Desktop.
function krava_insert_attachment( $file_handler, $post_id, $setthumb = 'false' ){
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
if ($setthumb) update_post_meta( $post_id, '_thumbnail_id', $attach_id );
return $attach_id;
}
if ( $_FILES ) {
$files = $_FILES['files'];
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]
);
$_FILES = array("files" => $file);
foreach ( $_FILES as $file => $array ) {
$newupload = krava_insert_attachment( $file, $post_id );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment