I would create an AJAX Call from the delete post Link.
First, register the AJAX function:
add_action( 'wp_ajax_wpse_delete_post', 'wpse_delete_post' );
I would not add the AJAX function to the nopriv Users, but if you want every visitor of the site (ignoring capabilities) to be able to delete a post, you would have to add this as well:
add_action( 'wp_ajax_nopriv_wpse_delete_post', 'wpse_delete_post' );
The next thing to do is define the callback-function:
function wpse_delete_post() {
// be sure to add all the security you need, so that no post gets deleted by accident or malicious intent
$postid = intval( $_POST['post_id'] );
// also define what you want your AJAX message to return
if ( wp_delete_post( $postid ) === false ) {
echo "fail";
} else {
echo "success";
}
die(); // needed to function properly
}
The final step would be the the AJAX call from your Link. Lets assume you have the ID available on your Link, just to make things easier:
<a href="#" id="POSTID" class="ajax_delete_post">delete post</a>
You add the following Javascript to your functions:
jQuery(document).ready(function($){
$(".ajax_delete_post").click( function(){
var data = {
action: 'wpse_delete_post',
post_id: $(this).val("id")
};
$.post(ajaxurl, data, function(response) {
console.log(response);
});
});
});
Be sure to have ajaxurl
defined as well, and you should be good to go.