Skip to content

Instantly share code, notes, and snippets.

@marcelo-ribeiro
Last active September 12, 2017 04:38
Show Gist options
  • Save marcelo-ribeiro/46d3906d330f4025aa35f4d4cfecb8e1 to your computer and use it in GitHub Desktop.
Save marcelo-ribeiro/46d3906d330f4025aa35f4d4cfecb8e1 to your computer and use it in GitHub Desktop.
WP REST API Upload Image
var settings = {
"async": true,
"crossDomain": true,
"url": "http://www.marcelo-ribeiro.tk/wp-json/wp/v2/media/48",
"method": "POST",
"headers": {
"authorization": "Basic bWFyY2Vsb3JpYmVpcm86QDg1ODM1OTU0Iw==",
"content-type": "application/json"
},
"processData": false,
"data": "{\n\t\"post\": 1,\n\t\"author\": 1,\n\t\"title\": \"Imagem carro\"\n}"
}
$.ajax(settings).done(function (response) {
console.log(response);
});
var form = new FormData();
form.append("file", $('input#file')[0].files[0]);
form.append("title", "Minha imagem");
form.append("post", "1");
form.append("author", "1");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://www.marcelo-ribeiro.tk/wp-json/wp/v2/media",
"method": "POST",
"headers": {
"authorization": "Basic bWFyY2Vsb3JpYmVpcm86QDg1ODM1OTU0Iw==",
"content-disposition": "attachment; filename=imagem_teste.jpg"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment