Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiechong/ed2b569fb879015f66c33eb2cbe6576d to your computer and use it in GitHub Desktop.
Save jamiechong/ed2b569fb879015f66c33eb2cbe6576d to your computer and use it in GitHub Desktop.
Ajax upload file to Wordpress REST API v2 media endpoint
<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