Created
March 21, 2012 23:15
-
-
Save dancameron/2154039 to your computer and use it in GitHub Desktop.
GBS: Upload and Set Attachment
This file contains 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
/** | |
* 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