Skip to content

Instantly share code, notes, and snippets.

@dancameron
Created March 21, 2012 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dancameron/2154039 to your computer and use it in GitHub Desktop.
Save dancameron/2154039 to your computer and use it in GitHub Desktop.
GBS: Upload and Set Attachment
/**
* Add a file as a post attachment.
*
* @return null
*/
public static function set_attachement( $post_id, $files ) {
if (!function_exists('wp_generate_attachment_metadata')){
require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');
}
foreach ($files as $file => $array) {
if ($files[$file]['error'] !== UPLOAD_ERR_OK) {
self::set_message('upload error : ' . $files[$file]['error']);
}
$attach_id = media_handle_upload( $file, $post_id );
}
// Make it a thumbnail while we're at it.
if ($attach_id > 0){
update_post_meta($post_id,'_thumbnail_id',$attach_id);
}
return $attach_id;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment