-
-
Save touchopia/79f450b4e8f5d789f0c2 to your computer and use it in GitHub Desktop.
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
| <?php | |
| function upload_user_file( $file = array() ) { | |
| require_once( ABSPATH . 'wp-admin/includes/admin.php' ); | |
| $file_return = wp_handle_upload( $file, array('test_form' => false ) ); | |
| if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) { | |
| return false; | |
| } else { | |
| $filename = $file_return['file']; | |
| $attachment = array( | |
| 'post_mime_type' => $file_return['type'], | |
| 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
| 'post_content' => '', | |
| 'post_status' => 'inherit', | |
| 'guid' => $file_return['url'] | |
| ); | |
| $attachment_id = wp_insert_attachment( $attachment, $file_return['url'] ); | |
| require_once(ABSPATH . 'wp-admin/includes/image.php'); | |
| $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); | |
| wp_update_attachment_metadata( $attachment_id, $attachment_data ); | |
| if( 0 < intval( $attachment_id ) ) { | |
| return $attachment_id; | |
| } | |
| } | |
| return false; | |
| } | |
| ?> |
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
| <?php | |
| if( ! empty( $_FILES ) ) { | |
| foreach( $_FILES as $file ) { | |
| if( is_array( $file ) ) { | |
| $attachment_id = upload_user_file( $file ); | |
| } | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment