Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! function_exists( 'wp_handle_upload' ) ) {
- require_once( ABSPATH . 'wp-admin/includes/file.php' );
- }
- $uploadedfile = $_FILES['image'];
- $filetmp = $_FILES['image']['tmp_name'];
- $image_info = pathinfo($_FILES['image']['name']);
- $ext = $image_info['extension'];
- $exclude_upload_file_array = array('(',')','[',']','{','}','\'','"','#',' ','@',',','.','+',':');
- $filetitle = str_replace($exclude_upload_file_array, '', $image_info['filename']);
- $upload_overrides = array( 'test_form' => false );
- $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
- if ( $movefile && ! isset( $movefile['error'] ) ) {
- $filedest = $movefile['file'];
- $fileurl = $movefile['url'];
- include( ABSPATH . 'wp-admin/includes/image.php' );
- $attachment = array('post_title'=>$filetitle,
- 'post_status'=>'inherit',
- 'post_name'=>$filetitle,
- 'post_type'=>'attachment'
- );
- if($ext){
- $attachment['post_mime_type']="image/".$ext;
- }
- $attach_id = wp_insert_attachment( $attachment, $filedest );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
- wp_update_attachment_metadata( $attach_id, $attach_data );
- }
- ---- Use Filter Code ----
- if ( ! function_exists( 'wp_handle_upload' ) ) {
- require_once( ABSPATH . 'wp-admin/includes/file.php' );
- }
- $uploadedfile = $_FILES['image'];
- $filetmp = $_FILES['image']['tmp_name'];
- $image_info = pathinfo($_FILES['image']['name']);
- $ext = $image_info['extension'];
- $exclude_upload_file_array = array('(',')','[',']','{','}','\'','"','#',' ','@',',','.','+',':');
- $filetitle = str_replace($exclude_upload_file_array, '', $image_info['filename']);
- $upload_overrides = array( 'test_form' => false );
- $movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
- if ( $movefile && ! isset( $movefile['error'] ) ) {
- $filedest = $movefile['file'];
- $fileurl = $movefile['url'];
- include( ABSPATH . 'wp-admin/includes/image.php' );
- $attachment = array('post_title'=>$filetitle,
- 'post_status'=>'inherit',
- 'post_name'=>$filetitle,
- 'post_type'=>'attachment'
- );
- if($ext){
- $attachment['post_mime_type']="image/".$ext;
- }
- $attach_id = wp_insert_attachment( $attachment, $filedest );
- $attach_data = wp_generate_attachment_metadata( $attach_id, $filedest );
- // Apply filter....
- apply_filters( 'wp_update_attachment_metadata', $attach_data, $attach_id );
- }
Add Comment
Please, Sign In to add comment