Guest User

Untitled

a guest
Jan 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. $attachment = array(
  2. 'guid' => $_POST['at_guid'],
  3. 'post_mime_type' => $_POST['at_mime'],
  4. 'post_title' => $_POST['at_title'],
  5. 'post_content' => '',
  6. 'post_status' => $_POST['at_status']
  7. );
  8.  
  9. $filename = $_POST['at_filename'];
  10. $post_id = $_POST['at_id'];
  11.  
  12.  
  13. $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
  14.  
  15. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  16. wp_update_attachment_metadata( $attach_id, $attach_data );
  17.  
  18. $delete_attachment = wp_delete_attachment( $attachment_id_clean );
  19.  
  20. // Check the type of file. We'll use this as the 'post_mime_type'.
  21. $filetype = wp_check_filetype( basename( $filename ), null );
  22.  
  23. // Get the path to the upload directory.
  24. $wp_upload_dir = wp_upload_dir();
  25.  
  26. // Prepare an array of post data for the attachment.
  27. $attachment = array(
  28. 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
  29. 'post_mime_type' => $filetype['type'],
  30. 'post_title' => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
  31. 'post_content' => '',
  32. 'post_status' => 'inherit'
  33. );
  34.  
  35. // Insert the attachment.
  36. $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
  37.  
  38. // Make sure that this file is included, as
  39. wp_generate_attachment_metadata() depends on it.
  40. require_once( ABSPATH . 'wp-admin/includes/image.php' );
  41.  
  42. // Generate the metadata for the attachment, and update the database record.
  43. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  44. $attachment_update = wp_update_attachment_metadata( $attach_id, $attach_data );
  45. if($attachment_update){
  46. //$result = var_dump($attach_data);
  47. $arr = array('result' => 'success', 'id' => $attach_id);
  48. $result = json_encode($arr);
  49. }
  50.  
  51. global $wpdb;
  52. $delete_attachment = $wpdb->update($wpdb->posts, array('post_parent'=>0), array('id'=>$attachment_id_clean, 'post_type'=>'attachment'));
Add Comment
Please, Sign In to add comment