Skip to content

Instantly share code, notes, and snippets.

@jennschiffer
Last active August 29, 2015 14:27
Show Gist options
  • Save jennschiffer/7542b6cea4999049c97f to your computer and use it in GitHub Desktop.
Save jennschiffer/7542b6cea4999049c97f to your computer and use it in GitHub Desktop.
wordpress custom meta upload for post and/or taxonomy question
<?php
/*
thnx for the help, EAL
*/
// acceptable file types
$supportedTypes = array('image/jpeg','image/gif','image/png','image/bmp','image/tiff');
for ( $i = 1; $i < 4; $i++ ) {
if ( !empty($_FILES['bocoup-work-secondary-images-' . $i]['name']) ) {
$uploadCheck = wp_check_filetype(basename($_FILES['bocoup-work-secondary-images-' . $i]['name']));
$uploadType = $uploadCheck['type'];
if(in_array($uploadType, $supportedTypes)) {
$mediaAttachment = media_handle_upload( ('bocoup-work-secondary-images-' . $i), $post_id );
if(isset($mediaAttachment['error']) && $mediaAttachment['error'] != 0) {
wp_die('There was an error uploading your file ' . $mediaAttachment['error']);
}
else {
// upload as attachment
add_post_meta($post_id, 'bocoup-work-secondary-images-' . $i, $mediaAttachment);
update_post_meta($post_id, 'bocoup-work-secondary-images-' . $i, $mediaAttachment);
}
$upload = '';
$uploadType = '';
}
else {
wp_die("Error: The upload file type must be in JPEG, GIF, PNG, BMP or TIFF.");
}
}
}
?>
@jennschiffer
Copy link
Author

this may be the ticket https://gist.github.com/hissy/7352933

@ericandrewlewis
Copy link

media_handle_upload() is in your future. wp_upload_bits() upload files but won't create an attachment object, media_handle_upload() will.

@jennschiffer
Copy link
Author

thaaaaaaaaaaaaaanks babe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment