-
-
Save eduplessis/dc530523f0b98ae233dc to your computer and use it in GitHub Desktop.
Create,update,delete post
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
$('#modal-new, #modal-new-time, #form-edit').on('click', '#btn-save, #btn-del', function(event) { | |
event.preventDefault(); | |
var formToSend = $(this).parents('.modal').find('form'); | |
var preType = $(this).parents('.modal').data('type'); | |
var urlSend = '/wp-json/posts/'; | |
var typeSend = 'POST'; | |
if ($(this).data('action') === 'edit') { | |
formToSend = $(this).parents('form'); | |
preType = formToSend.data('type'); | |
urlSend = '/wp-json/posts/' + formToSend.data('id'); | |
typeSend = 'PUT'; | |
}; | |
if ($(this).data('action') === 'delete') { | |
formToSend = $(this).parents('form'); | |
preType = formToSend.data('type'); | |
urlSend = '/wp-json/posts/' + formToSend.data('id') + '/?force=true'; | |
typeSend = 'DELETE'; | |
}; | |
var postMeta = []; | |
var data = { | |
title: '' + formToSend.find('#title').val(), | |
content: '', | |
postMeta: postMeta | |
} | |
debugger | |
$.each(formToSend[0], function(index, val) { | |
if ($(this).val() !== '' && $(this).attr('data-meta') ==='post_meta' ) { | |
postMeta.push( | |
{ | |
key: '' + preType + '_' + $(this).attr('id'), | |
value: $(this).val() | |
} | |
); | |
}; | |
}); | |
postMeta.push( | |
{ | |
key: '' + preType + '_color', | |
value: randomColor() | |
} | |
); | |
debugger | |
if ( data.title !== '' ) { | |
$.ajax({ | |
url: urlSend, | |
beforeSend:function(xhr){ | |
xhr.setRequestHeader('X-WP-Nonce', WP_API_Settings.nonce); | |
}, | |
type: typeSend, | |
dataType: 'json', | |
data: { | |
title: data.title, | |
content_raw: data.content, | |
type: preType, | |
status: 'publish', | |
post_meta: data.postMeta | |
}, | |
}) | |
.done(function(data) { | |
console.log(data); | |
$('#modal-new, #modal-new-time').modal('hide'); | |
}) | |
.fail(function(jqXHR, textStatus ) { | |
console.log("error" + textStatus ); | |
}) | |
.always(function() { | |
console.log("complete"); | |
}); | |
}; | |
debugger; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment