Advertisement
keha76

WP Insert Attachment

Jan 28th, 2013
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. /**
  2.  * Insert Attachment
  3.  *
  4.  * @access private
  5.  * @since 0.1.3a
  6.  * @see shortcode_insert_recipe
  7.  *
  8.  * @param string $file_handler
  9.  * @param integer $post_id
  10.  * @param boolean $set_thumbnails
  11.  * @return integer|boolean
  12.  */
  13. private function insert_attachment( $file_handler, $post_id, $set_thumbnails = true )
  14. {
  15.     // check to make sure its a successful upload
  16.     if ( UPLOAD_ERR_OK !== $_FILES[ $file_handler ]['error']  ) {
  17.         __return_false();
  18.     }
  19.    
  20.     // Include required files from wp-admin to make this work
  21.     require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
  22.     require_once( ABSPATH . "wp-admin" . '/includes/file.php' );
  23.     require_once( ABSPATH . "wp-admin" . '/includes/media.php' );
  24.    
  25.     $attachment_id = media_handle_upload( $file_handler, $post_id );
  26.    
  27.     // Check if thumbnails should be generated
  28.     if ( $set_thumbnails )
  29.     {
  30.         update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
  31.     }
  32.    
  33.     return $attachment_id;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement