false ) ); $filename = wpkeyvalue( $file_return, 'file' ); $post_title = preg_replace( '/\.[^.]+$/', '', basename( $filename ) ); $attachment = array( 'post_mime_type' => wpkeyvalue( $file_return, 'type' ), 'post_title' => $post_title, 'post_content' => '', 'post_status' => 'inherit', 'guid' => wpkeyvalue( $wp_upload_dir, 'url' ) . '/' . basename($filename) ); global $wpdb, $current_user; $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'fields' => 'ids', 'author' => wpstdclass( $current_user, 'ID' ), 'meta_key' => wppost( 'key' ), 'meta_value' => 1, 'post_status' => array( 'publish', 'private', 'inherit' ), ); $query = new WP_Query( $args ); $query->posts[] = wppost( 'attach_id' ); if( $posts = array_filter( $query->posts ) ){ foreach ( $posts as $key => $post ) { wp_delete_attachment( $post, true ); } } $attach_id = wp_insert_attachment( $attachment, $filename, 289 ); require_once(ABSPATH . 'wp-admin/includes/image.php'); $attach_data = wp_generate_attachment_metadata( $attach_id, $filename ); wp_update_attachment_metadata( $attach_id, $attach_data ); $image_src = wp_get_attachment_image_src( $attach_id, 'full' ); update_post_meta( $attach_id, wppost( 'key' ), true ); update_user_meta( wpstdclass( $current_user, 'ID' ), 'ae_' . wppost( 'key' ), $attach_id ); $a = array( 'user_id' => wpstdclass( $current_user, 'ID' ), 'post_title' => $post_title, 'post_attach_id' => wppost( 'attach_id' ), 'attach_id' => $attach_id, 'attach_url' => wpkeyvalue( $image_src, 0 ), 'key' => wppost( 'key' ) ); echo json_encode( $a );