Skip to content

Instantly share code, notes, and snippets.

@vladfil
Last active August 31, 2017 12:12
Show Gist options
  • Select an option

  • Save vladfil/e35fb592435a0e4307b22dea22fe7f6e to your computer and use it in GitHub Desktop.

Select an option

Save vladfil/e35fb592435a0e4307b22dea22fe7f6e to your computer and use it in GitHub Desktop.
// Create post
$('#send-post').click( function() {
var title = $('.add-posts input[name="title"]').val();
var post = $('.add-posts textarea[name="post"]').val();
console.log(screenReaderText.nonce);
$.ajax({
type: 'POST',
url: 'http://wordpress.localhost.dev/wp-json/wp/v2/posts',
beforeSend: function(jqXHR){
jqXHR.setRequestHeader('X-WP-Nonce', screenReaderText.nonce);
},
data: {
title: title,
content: post,
status: 'publish'
},
});
} );
// Update post
$('#update-post').click( function() {
var title = $('.update-posts input[name="title"]').val();
var post = $('.update-posts textarea[name="post"]').val();
// console.log(screenReaderText.nonce);
var id = $(this).data('id');
var url = 'http://wordpress.localhost.dev/wp-json/wp/v2/posts/' + id;
$.ajax({
type: 'POST',
url: url,
beforeSend: function(jqXHR){
jqXHR.setRequestHeader('X-WP-Nonce', screenReaderText.nonce);
},
data: {
title: title,
content: post,
status: 'publish'
},
});
} );
// Delete post
$('#delete-post').click( function() {
var id = $(this).data('id');
var url = 'http://wordpress.localhost.dev/wp-json/wp/v2/posts/' + id;
$.ajax({
type: 'DELETE',
url: url,
beforeSend: function(jqXHR){
jqXHR.setRequestHeader('X-WP-Nonce', screenReaderText.nonce);
},
});
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment