Next, let us add javascript to make this form functional. The script below must be enqueued/loaded after jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | jQuery(document).ready( function (){ jQuery( '#submit-button' ).click( function (){ // create a FormData object and append all files to it var formData = new FormData(); var inputName, file; var urlToPhpFile = 'server.php' ; jQuery.each(jQuery( 'input[type="file"]' ), function (index, value) { inputName = jQuery(value).attr( 'name' ); file = jQuery(value).prop( 'files' )[0]; if (file) { formData.append(inputName, file); } jQuery.ajax(urlToPhpFile, { data: formData, type: 'post' , processData: false , contentType: false }).fail( function (errObj, errText, errThrown) { alert( 'Error!' ); }).done( function (data){ if (data.status == 0){ alert( 'Success!' ); } else { alert( 'Failed!' ); } }); }); }); }); |
At this stage we have a HTML form that will send the uploaded file to a php file at the root of wordpress installation using AJAX, when submit button is clicked.
Next, lets create a file server.php at root of our wordpress installation. Since this file will be executed outside of normal wordpress load sequence, we need an extra step to make wordpress functions available here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php //adjust path of "wp-load.php" and "file.php" based on location of this code. Here we are assuming //this code is written in a file at root of wordpress installation. require ( './wp-load.php' ); require_once (ABSPATH . 'wp-admin/includes/file.php' ); //@todo: add security and validations $file = $_FILES [ 'myfile' ]; $uploaded_file = wp_handle_upload( $file , array ( 'test_form' => false)); if ( $uploaded_file && ! isset( $uploaded_file [ 'error' ] ) ) { wp_send_json( array ( 'status' => '0' , 'message' => 'Success!' , 'uploadedFile' => $uploaded_file [ 'url' ] )); } else { wp_send_json( array ( 'status' => '-1' , 'message' => 'Failed!' )); } |
The above php file will receive incoming post data and upload the file in wordpress upload directory and will give the url of uploaded file. If other file details are needed, we can use different values of $uploaded_file
array