<?php if (! function_exists ( 'wp_handle_upload' )) require_once (ABSPATH . 'wp-admin/includes/file.php'); $uploadedfile = $_FILES ['myfile']; $upload_overrides = array ( 'test_form' => false ); //uploadm=ning file $movefile = wp_handle_upload ( $uploadedfile, $upload_overrides ); if ($movefile) { //adding to media library $wp_filetype = $movefile ['type']; $filename = $movefile ['file']; $wp_upload_dir = wp_upload_dir (); $url = $wp_upload_dir ['url'] . '/' . basename ( $filename ); $new_filename = basename ( $filename ); $attachment = array ( 'guid' => $url, 'post_mime_type' => $wp_filetype, 'post_title' => preg_replace ( '/\.[^.]+$/', '', $new_filename ), 'post_content' => '', 'post_status' => 'inherit' ); $attach_id = wp_insert_attachment ( $attachment, $filename ); } else { //error uploading } ?>