Created
January 24, 2026 03:26
-
-
Save Jerl92/5b1a936c4063e234be9be5d564ec0922 to your computer and use it in GitHub Desktop.
Wordpress front-end media upload
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" enctype="multipart/form-data"> | |
| <!-- Security field --> | |
| <?php wp_nonce_field( 'media_upload', 'media_upload_nonce' ); ?> | |
| <label for="file_upload">Select File:</label> | |
| <input type="file" name="file_upload" id="file_upload" required /> | |
| <input type="hidden" name="action" value="frontend_media_upload"> | |
| <input type="submit" name="submit" value="Upload Media" /> | |
| </form> | |
| <?php | |
| function handle_frontend_media_upload() { | |
| // Verify nonce for security | |
| if ( ! isset( $_POST['media_upload_nonce'] ) || ! wp_verify_nonce( $_POST['media_upload_nonce'], 'media_upload' ) ) { | |
| wp_die( 'Security check failed.' ); | |
| } | |
| // Check if file was uploaded | |
| if ( empty( $_FILES['file_upload'] ) ) { | |
| wp_die( 'No file selected.' ); | |
| } | |
| // Required WordPress files for media handling | |
| require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
| require_once( ABSPATH . 'wp-admin/includes/media.php' ); | |
| // Handle the upload using the built-in WordPress function | |
| $upload = wp_handle_upload( $_FILES['file_upload'], array( 'test_form' => false ) ); | |
| if ( ! empty( $upload['error'] ) ) { | |
| wp_die( $upload['error'] ); | |
| } | |
| // Insert the uploaded file into the media library | |
| $attachment_id = wp_insert_attachment( | |
| array( | |
| 'guid' => $upload['url'], | |
| 'post_mime_type' => $upload['type'], | |
| 'post_title' => basename( $upload['file'] ), | |
| 'post_content' => '', | |
| 'post_status' => 'publish', | |
| ), | |
| $upload['file'] | |
| ); | |
| if ( is_wp_error( $attachment_id ) || ! $attachment_id ) { | |
| wp_die( 'Error adding file to media library.' ); | |
| } | |
| // Generate metadata and image sizes (essential for images) | |
| wp_update_attachment_metadata( | |
| $attachment_id, | |
| wp_generate_attachment_metadata( $attachment_id, $upload['file'] ) | |
| ); | |
| // Redirect the user after successful upload | |
| wp_safe_redirect( home_url( '/curriculum-vitae/' ) ); // Redirect back to the front page | |
| exit; | |
| } | |
| // Hook the function to the 'admin_post_' action specified in the form | |
| add_action( 'admin_post_frontend_media_upload', 'handle_frontend_media_upload' ); | |
| add_action( 'admin_post_nopriv_frontend_media_upload', 'handle_frontend_media_upload' ); // For logged out users | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment