Guest User

Untitled

a guest
Jun 1st, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1.  
  2.  
  3. if ( ! function_exists( 'wp_handle_upload' ) ) {
  4. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  5. }
  6.  
  7. $uploadedfile = $_FILES['image'];
  8. $filetmp = $_FILES['image']['tmp_name'];
  9.  
  10. $image_info = pathinfo($_FILES['image']['name']);
  11. $ext = $image_info['extension'];
  12.  
  13. $exclude_upload_file_array = array('(',')','[',']','{','}','\'','"','#',' ','@',',','.','+',':');
  14. $filetitle = str_replace($exclude_upload_file_array, '', $image_info['filename']);
  15.  
  16. $upload_overrides = array( 'test_form' => false );
  17.  
  18. $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
  19.  
  20. if ( $movefile && ! isset( $movefile['error'] ) ) {
  21. $filedest = $movefile['file'];
  22. $fileurl = $movefile['url'];
  23.  
  24. include( ABSPATH . 'wp-admin/includes/image.php' );
  25.  
  26. $attachment = array('post_title'=>$filetitle,
  27. 'post_status'=>'inherit',
  28. 'post_name'=>$filetitle,
  29. 'post_type'=>'attachment'
  30. );
  31.  
  32. if($ext){
  33. $attachment['post_mime_type']="image/".$ext;
  34. }
  35.  
  36. $attach_id = wp_insert_attachment( $attachment, $filedest );
  37.  
  38. $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
  39. wp_update_attachment_metadata( $attach_id, $attach_data );
  40. }
  41.  
  42.  
  43. ---- Use Filter Code ----
  44.  
  45. if ( ! function_exists( 'wp_handle_upload' ) ) {
  46. require_once( ABSPATH . 'wp-admin/includes/file.php' );
  47. }
  48.  
  49. $uploadedfile = $_FILES['image'];
  50. $filetmp = $_FILES['image']['tmp_name'];
  51.  
  52. $image_info = pathinfo($_FILES['image']['name']);
  53. $ext = $image_info['extension'];
  54.  
  55. $exclude_upload_file_array = array('(',')','[',']','{','}','\'','"','#',' ','@',',','.','+',':');
  56. $filetitle = str_replace($exclude_upload_file_array, '', $image_info['filename']);
  57.  
  58. $upload_overrides = array( 'test_form' => false );
  59.  
  60. $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
  61.  
  62. if ( $movefile && ! isset( $movefile['error'] ) ) {
  63. $filedest = $movefile['file'];
  64. $fileurl = $movefile['url'];
  65.  
  66. include( ABSPATH . 'wp-admin/includes/image.php' );
  67.  
  68. $attachment = array('post_title'=>$filetitle,
  69. 'post_status'=>'inherit',
  70. 'post_name'=>$filetitle,
  71. 'post_type'=>'attachment'
  72. );
  73.  
  74. if($ext){
  75. $attachment['post_mime_type']="image/".$ext;
  76. }
  77.  
  78. $attach_id = wp_insert_attachment( $attachment, $filedest );
  79.  
  80. $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
  81.  
  82. // Apply filter....
  83. apply_filters( 'wp_update_attachment_metadata', $attach_data, $attach_id );
  84. }
Add Comment
Please, Sign In to add comment