Created
July 20, 2017 22:01
-
-
Save jamiechong/ed2b569fb879015f66c33eb2cbe6576d to your computer and use it in GitHub Desktop.
Ajax upload file to Wordpress REST API v2 media endpoint
This file contains 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
<input type="file" id="file"/> | |
----- | |
var file = $('#file')[0].files[0]; | |
$.ajax({ | |
method: "POST", | |
url: REST_API_URL + 'wp/v2/media', | |
data: file, | |
processData: false, | |
contentType: false, | |
beforeSend: function (xhr) { | |
xhr.setRequestHeader('X-WP-Nonce', MP_FORM_SUBMIT.nonce); | |
xhr.setRequestHeader('Content-Disposition', 'attachment; filename='+file.name); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment