Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $attachment = array(
- 'guid' => $_POST['at_guid'],
- 'post_mime_type' => $_POST['at_mime'],
- 'post_title' => $_POST['at_title'],
- 'post_content' => '',
- 'post_status' => $_POST['at_status']
- );
- $filename = $_POST['at_filename'];
- $post_id = $_POST['at_id'];
- $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- $delete_attachment = wp_delete_attachment( $attachment_id_clean );
- // Check the type of file. We'll use this as the 'post_mime_type'.
- $filetype = wp_check_filetype( basename( $filename ), null );
- // Get the path to the upload directory.
- $wp_upload_dir = wp_upload_dir();
- // Prepare an array of post data for the attachment.
- $attachment = array(
- 'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
- 'post_mime_type' => $filetype['type'],
- 'post_title' => preg_replace( '/.[^.]+$/', '', basename( $filename ) ),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- // Insert the attachment.
- $attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
- // Make sure that this file is included, as
- wp_generate_attachment_metadata() depends on it.
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
- // Generate the metadata for the attachment, and update the database record.
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
- $attachment_update = wp_update_attachment_metadata( $attach_id, $attach_data );
- if($attachment_update){
- //$result = var_dump($attach_data);
- $arr = array('result' => 'success', 'id' => $attach_id);
- $result = json_encode($arr);
- }
- global $wpdb;
- $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