Upload files from a custom form to the WordPress media library
Sometimes files come from a form on the site rather than from the admin: a photo for a listing, a CV for a job application, an avatar. WordPress has the functions to check such a file, move it to the uploads folder and create a media library entry with all image sizes, so you do not have to work with move_uploaded_file() yourself.
Problem: A front-end form must accept an image and save it in the media library, attached to a post.
Solution: Check the nonce and the user’s rights, then call media_handle_upload(): it runs wp_handle_upload() (type check, unique file name, move), wp_insert_attachment() and wp_generate_attachment_metadata() in one step. Use the lower-level functions only when the file must not go into the media library.
The form
<form method="post" enctype="multipart/form-data">
<?php wp_nonce_field( 'ha_upload', 'ha_upload_nonce' ); ?>
<input type="file" name="ha_file" accept="image/jpeg,image/png" required>
<button type="submit">Upload</button>
</form>
Without enctype="multipart/form-data" the browser sends only the file name.
Handling the upload
<?php
add_action( 'template_redirect', 'ha_handle_upload' );
function ha_handle_upload() {
if ( empty( $_FILES['ha_file'] ) || empty( $_POST['ha_upload_nonce'] )
|| ! wp_verify_nonce( $_POST['ha_upload_nonce'], 'ha_upload' ) ) {
return;
}
if ( ! current_user_can( 'upload_files' ) ) {
wp_die( 'You are not allowed to upload files.' );
}
// These files are loaded only in the admin, include them on the front end
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
require_once ABSPATH . 'wp-admin/includes/media.php';
$post_id = get_queried_object_id(); // attach to the current post, 0 = not attached
$attachment_id = media_handle_upload( 'ha_file', $post_id, array(), array(
'test_form' => false,
'mimes' => array( 'jpg|jpeg' => 'image/jpeg', 'png' => 'image/png' ), // only these types
) );
if ( is_wp_error( $attachment_id ) ) {
wp_die( esc_html( $attachment_id->get_error_message() ) );
}
set_post_thumbnail( $post_id, $attachment_id ); // optional: make it the featured image
wp_safe_redirect( add_query_arg( 'uploaded', 1, get_permalink( $post_id ) ) );
exit;
}
The mimes override limits the accepted types for this form only. WordPress checks the real content of images, not just the extension, so a PHP file renamed to photo.jpg is rejected.
Without the media library
To store a file in uploads without creating an attachment, for example an import file that a plugin processes once, use wp_handle_upload() alone:
<?php
$result = wp_handle_upload( $_FILES['ha_file'], array(
'test_form' => false,
'mimes' => array( 'csv' => 'text/csv' ),
) );
if ( isset( $result['error'] ) ) {
wp_die( esc_html( $result['error'] ) );
}
// $result['file'] = absolute path, $result['url'] = URL, $result['type'] = MIME type
Files saved this way are not visible in Media, so delete them yourself when they are no longer needed.
NOTE: Never let guests upload without limits. A form open to everybody needs at least the type list, a size check ($_FILES['ha_file']['size']) and spam protection; otherwise it becomes free file hosting. For logged-in users, the upload_files capability (authors and above by default) is the right check.